00001 #include <time.h>
00002 #include <algorithm>
00003 #include <FCam/TSQueue.h>
00004
00005 bool done = false;
00006
00007 void *runAThread(void *arg) {
00008 FCam::TSQueue<int> *test = (FCam::TSQueue<int> *)arg;
00009 int counter = 0;
00010 while (!done) {
00011 test->push(counter);
00012 timespec tSleep;
00013 tSleep.tv_sec = 0;
00014 tSleep.tv_nsec = 10e6;
00015 nanosleep(&tSleep, NULL);
00016 counter++;
00017 }
00018 }
00019
00020 void *runBThread(void *arg) {
00021 FCam::TSQueue<int> *test = (FCam::TSQueue<int> *)arg;
00022 int counter = 1000000;
00023 while (!done) {
00024 test->push(counter);
00025 timespec tSleep;
00026 tSleep.tv_sec = 0;
00027 tSleep.tv_nsec = 5e6;
00028 nanosleep(&tSleep, NULL);
00029 counter++;
00030 }
00031 }
00032
00033 int main(int argc, char **argv) {
00034
00035 FCam::TSQueue<int> test;
00036
00037 pthread_t aThread, bThread;
00038
00039 pthread_create(&aThread, NULL, runAThread, (void *)&test);
00040 pthread_create(&bThread, NULL, runBThread, (void *)&test);
00041
00042 for (int i=0; i < 200; i++) {
00043 int n1 = test.pull();
00044 int n2 = test.pull();
00045 printf("\t%d\t%d\n",n1,n2);
00046 }
00047
00048 timespec tSleep;
00049 tSleep.tv_sec = 1;
00050 tSleep.tv_nsec = 0;
00051 nanosleep(&tSleep, NULL);
00052
00053 {
00054 FCam::TSQueue<int>::locking_iterator it = test.begin();
00055 printf("entries before sort: %d\n", test.size());
00056 std::sort(it, test.end());
00057 for(int k=0; it != test.end(); it++, k++) {
00058 if (k % 4 == 0) printf("\n");
00059 printf("%d\t", *it);
00060 }
00061 printf("entries after sort: %d\n", test.size());
00062 tSleep.tv_sec = 0;
00063 tSleep.tv_nsec = 100e6;
00064 nanosleep(&tSleep, NULL);
00065 printf("entries after sort, should still be locked: %d\n", test.size());
00066 }
00067
00068 tSleep.tv_sec = 0;
00069 tSleep.tv_nsec = 100e6;
00070 nanosleep(&tSleep, NULL);
00071
00072 printf("entries after sleeping a bit (should be more): %d\n", test.size());
00073
00074 done = true;
00075 pthread_join(aThread, NULL);
00076 pthread_join(bThread, NULL);
00077 return 1;
00078 }