qa_barrier.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <core/threading/thread.h>
00027 #include <core/threading/barrier.h>
00028
00029 #include <iostream>
00030 #include <string>
00031
00032 using namespace std;
00033 using namespace fawkes;
00034
00035 class ExampleBarrierThread : public Thread
00036 {
00037 public:
00038 ExampleBarrierThread(string pp,
00039 Barrier *barrier, unsigned int sleep_time)
00040 : Thread("ExampleBarrierThread", Thread::OPMODE_CONTINUOUS)
00041 {
00042 this->pp = pp;
00043 this->barrier = barrier;
00044 this->sleep_time = sleep_time;
00045 }
00046
00047 virtual void loop()
00048 {
00049 usleep( sleep_time );
00050 cout << pp << ": Waiting for barrier" << endl;
00051 barrier->wait();
00052 cout << pp << ": Barrier lifted" << endl;
00053 }
00054
00055 private:
00056 Barrier *barrier;
00057 unsigned int sleep_time;
00058 string pp;
00059
00060 };
00061
00062
00063 int
00064 main(int argc, char **argv)
00065 {
00066 Barrier *b = new Barrier(3);
00067
00068 ExampleBarrierThread *t1 = new ExampleBarrierThread("t1", b, 3424345);
00069 ExampleBarrierThread *t2 = new ExampleBarrierThread("t2", b, 326545);
00070 ExampleBarrierThread *t3 = new ExampleBarrierThread("t3", b, 6458642);
00071
00072 t1->start();
00073 t2->start();
00074 t3->start();
00075
00076 t1->join();
00077 t2->join();
00078 t3->join();
00079
00080 delete t1;
00081 delete t2;
00082 delete t3;
00083
00084 delete b;
00085 }
00086
00087
00088