Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00013 #include "thread-queue.h"
00014 #include "assert.h"
00015 #include <boost/thread.hpp>
00016 #include <unistd.h>
00017 #include <iostream>
00018 #include "utils/io-lock.h"
00019
00020 using namespace std;
00021
00022 thread_queue<int> b;
00023
00024 void producer(int slowness, int size) {
00025 int i = 1;
00026 L_COUT << "// TEST-IGNORE: Start producing" << endl;
00027 for (; i <= size; i++) {
00028
00029 usleep(i * slowness);
00030 b.push(i);
00031 }
00032 b.push(-1);
00033 for (; i <= 2 * size; i++) {
00034 b.push(i);
00035 }
00036 b.push(-2);
00037 };
00038
00039 void consumer(int slowness) {
00040 int i = 1;
00041 L_COUT << "Start consuming" << endl;
00042 while(b.front() != -1) {
00043 int f = b.pop_front();
00044 (void)f;
00045 assert(f == i); ++i;
00046 }
00047 L_COUT << "First batch done" << endl;
00048 b.pop();
00049 while(b.front() != -2) {
00050 assert(b.front() == i); ++i;
00051 usleep(b.front() * slowness);
00052 b.pop();
00053 }
00054 b.pop();
00055 };
00056
00057 void run_test(int pslow, int cslow, int size) {
00058 boost::thread p(producer, pslow, size);
00059 boost::thread c(consumer, cslow);
00060
00061 p.join();
00062 c.join();
00063 }
00064
00065 int main () {
00066 b.push(42);
00067 assert(b.front() == 42);
00068 b.push(43);
00069 assert(b.front() == 42);
00070 assert(b.back() == 43);
00071 b.pop();
00072 assert(b.front() == 43);
00073 b.pop();
00074
00075 L_COUT << "Threads, producer slower" << endl;
00076 run_test(300, 1, 50);
00077
00078 L_COUT << "Threads, producer much slower" << endl;
00079 run_test(3000, 1, 3);
00080
00081 L_COUT << "Threads, consumer slower" << endl;
00082 run_test(1, 300, 100);
00083
00084 L_COUT << "Threads, same speed" << endl;
00085 run_test(1, 1, 100);
00086 }