Fawkes API  Fawkes Development Version
qa_barrier.cpp
1 
2 /***************************************************************************
3  * example_barrier.cpp - barrier example program
4  *
5  * Created: Thu Sep 15 14:48:11 2006
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 // Do not mention in API doc
24 /// @cond EXAMPLES
25 
26 #include <core/threading/thread.h>
27 #include <core/threading/barrier.h>
28 
29 #include <iostream>
30 #include <string>
31 
32 using namespace std;
33 using namespace fawkes;
34 
35 class ExampleBarrierThread : public Thread
36 {
37  public:
38  ExampleBarrierThread(string pp,
39  Barrier *barrier, unsigned int sleep_time)
40  : Thread("ExampleBarrierThread", Thread::OPMODE_CONTINUOUS)
41  {
42  this->pp = pp;
43  this->barrier = barrier;
44  this->sleep_time = sleep_time;
45  }
46 
47  virtual void loop()
48  {
49  usleep( sleep_time );
50  cout << pp << ": Waiting for barrier" << endl;
51  barrier->wait();
52  cout << pp << ": Barrier lifted" << endl;
53  }
54 
55  private:
56  Barrier *barrier;
57  unsigned int sleep_time;
58  string pp;
59 
60 };
61 
62 
63 int
64 main(int argc, char **argv)
65 {
66  Barrier *b = new Barrier(3);
67 
68  ExampleBarrierThread *t1 = new ExampleBarrierThread("t1", b, 3424345);
69  ExampleBarrierThread *t2 = new ExampleBarrierThread("t2", b, 326545);
70  ExampleBarrierThread *t3 = new ExampleBarrierThread("t3", b, 6458642);
71 
72  t1->start();
73  t2->start();
74  t3->start();
75 
76  t1->join();
77  t2->join();
78  t3->join();
79 
80  delete t1;
81  delete t2;
82  delete t3;
83 
84  delete b;
85 }
86 
87 
88 /// @endcond
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32