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/barrier.h>
27#include <core/threading/thread.h>
28
29#include <iostream>
30#include <string>
31
32using namespace std;
33using namespace fawkes;
34
35class ExampleBarrierThread : public Thread
36{
37public:
38 ExampleBarrierThread(string pp, Barrier *barrier, unsigned int sleep_time)
39 : Thread("ExampleBarrierThread", Thread::OPMODE_CONTINUOUS)
40 {
41 this->pp = pp;
42 this->barrier = barrier;
43 this->sleep_time = sleep_time;
44 }
45
46 virtual void
47 loop()
48 {
49 usleep(sleep_time);
50 cout << pp << ": Waiting for barrier" << endl;
51 barrier->wait();
52 cout << pp << ": Barrier lifted" << endl;
53 }
54
55private:
56 Barrier * barrier;
57 unsigned int sleep_time;
58 string pp;
59};
60
61int
62main(int argc, char **argv)
63{
64 Barrier *b = new Barrier(3);
65
66 ExampleBarrierThread *t1 = new ExampleBarrierThread("t1", b, 3424345);
67 ExampleBarrierThread *t2 = new ExampleBarrierThread("t2", b, 326545);
68 ExampleBarrierThread *t3 = new ExampleBarrierThread("t3", b, 6458642);
69
70 t1->start();
71 t2->start();
72 t3->start();
73
74 t1->join();
75 t2->join();
76 t3->join();
77
78 delete t1;
79 delete t2;
80 delete t3;
81
82 delete b;
83}
84
85/// @endcond
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.