| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

tutorial.cpp
Go to the documentation of this file.
1
16 //#define _CRTDBG_MAP_ALLOC
17 #include <stdlib.h>
18
19 #include <iostream>
20 #include <sstream>
21 #include <boost/thread/mutex.hpp>
22 #include <boost/bind.hpp>
23 #include <boost/threadpool.hpp>
24
25 using namespace std;
26 using namespace boost::threadpool;
27
28 //
29 // Helpers
30 boost::mutex m_io_monitor;
31
32 void print(string text)
33 {
34 boost::mutex::scoped_lock lock(m_io_monitor);
35 cout << text;
36 }
37
38 template<typename T>
39 string to_string(T const & value)
40 {
41 ostringstream ost;
42 ost << value;
43 ost.flush();
44 return ost.str();
45 }
46
47
48
49 //
50 // An example task functions
51 void task_1()
52 {
53 print(" task_1()\n");
54 throw 5;
55 }
56
57 void task_2()
58 {
59 print(" task_2()\n");
60 throw 5;
61 }
62
63 void task_3()
64 {
65 print(" task_3()\n");
66 }
67
68 void task_with_parameter(int value)
69 {
70 print(" task_with_parameter(" + to_string(value) + ")\n");
71 }
72
73 int loops = 0;
74 bool looped_task()
75 {
76 print(" looped_task()\n");
77 return ++loops < 5;
78 }
79
80
81 int task_int_23()
82 {
83 print(" task_int_23()\n");
84 return 23;
85 }
86
87 int task_int_1()
88 {
89 print(" task_int_1()\n");
90 return 1;
91 }
92
93
94 class CTest
95 {
96 pool m_pool;
97 public:
98 CTest()
99 : m_pool(pool(1000))
100 {
101 }
102 };
103
104
105 //
106 // A demonstration of the thread_pool class
107 int main (int , char * const [])
108 {
109 print("\nWelcome to the threadpool tutorial!\n");
110
111 print("\n**************************************\n");
112 print("Section 1: Quick Start\n");
113
114 //void func()
115 {
116 print(" Create a new thread pool\n");
117 pool tp(2); // tp is handle to the pool
118
119 // Add tasks
120 tp.schedule(&task_1);
121 tp.schedule(&task_2);
122 tp.schedule(&task_3);
123 tp.schedule(boost::bind(task_with_parameter, 4));
124
125 // The pool handle tp is allocated on stack and will
126 // be destructed if it gets out of scope. Before the
127 // pool is destroyed it waits for its tasks.
128 // That means the current thread of execution is
129 // blocked at the end of the function
130 // (until all tasks are processed).
131 // while (&tp){int i = 3; ++i;}
132 }
133
134 { // Section Futures
135 print("\n**************************************\n");
136 print("Section 1: Futures\n");
137
138 //typedef thread_pool<task_func, fifo_scheduler, static_size, empty_controller, wait_for_all_tasks> test_pool;
139
140 pool tp;
141
142 // tp.resize(0);
143 tp.pending();
144 // tp.clear();
145 boost::xtime t;
146 tp.wait(t);
147 bool test = tp.empty();
148 if(test)
149 {
150 test = false;
151 }
152
153 tp.size_controller().resize(2);
154
155 //test_pool::size_controller_type controller = tp.size_controller();
156 // controller.resize(5);
157
158 schedule(tp, &task_int_1);
159 future<int> res = schedule(tp, &task_int_23);
160 future<int> res2 = schedule(tp, &task_int_1);
161
162 res.wait();
163 int value = res.get() + res2.get();
164
165 res.cancel();
166 res.is_cancelled();
167 value ++;
168
169 //thread_pool<boost::function0<int>, fifo_scheduler> test2332;
170
171 //TODO runnable comile test
172 }
173
174
175
176 { // Section 2
177 print("\n**************************************\n");
178 print("Section 2: Controlling scheduling\n");
179
180 // Create a lifo_pool: last task in, first task out
181 lifo_pool tp(0);
182
183 print(" Add tasks (using the pool's schedule function)\n");
184 schedule(tp, &task_1);
185 schedule(tp, &task_2);
186 schedule(tp, &task_3);
187
188 // tp.wait(); This would be a deadlock as there are no threads which process the tasks.
189
190 print(" Add some threads ...\n");
191 tp.size_controller().resize(5);
192
193 print(" Wait until all tasks are finished ...\n");
194 tp.wait();
195 print(" Tasks finished!\n");
196 }
197
198
199
200 { // Section 3:
201 print("\n**************************************\n");
202 print("Section 3: Prioritized Tasks\n");
203
204 prio_pool tp(0);
205
206
207 print(" Add prioritized tasks ...\n");
208 schedule(tp, prio_task_func(1, &task_1));
209 schedule(tp, prio_task_func(10,&task_2));
210 schedule(tp, prio_task_func(5,&task_3));
211
212 // Tasks are ordered according to their priority: task_2, task_4, task_3, task_1
213
214 print(" Thread added\n");
215 tp.size_controller().resize(10);
216
217 print(" Wait until all tasks are finished ...\n");
218 tp.wait();
219 print(" Tasks finished!\n");
220 }
221
222
223 /* */
224 { // Section 5:
225 print("\n**************************************\n");
226 print("Section 5: Advanced thread pool instantiation\n");
227 // Create the pool directly
228 /*
229 TODO
230 boost::shared_ptr<fifo_pool> tp = fifo_pool::create_pool(5);
231
232 print(" Add tasks ...\n");
233 tp->schedule(&task_1);
234 tp->schedule(&task_2);
235 tp->schedule(&task_3);
236 tp->schedule(looped_task_func(&looped_task, 1500));
237
238 print(" Wait until all tasks are finished ...\n");
239 tp->wait();
240 */
241
242 print(" Tasks finished!\n");
243
244 }
245
246
247 print("\n**************************************\n");
248 print("Tutorial finished!\n");
249
250
251
252 { // Section Compile Tests
253 print("\n**************************************\n");
254 print("Section Compile Tests\n");
255
256
257 fifo_pool tp;
258 tp.size_controller().resize(0);
259 tp.empty();
260 }
261
262 return 0;
263 }
boost::threadpool::thread_pool::wait
void wait(size_t task_threshold=0) const
The current thread of execution is blocked until the sum of all active and pending tasks is equal or ...
Definition: pool.hpp:176
task_int_1
int task_int_1()
Definition: tutorial.cpp:87
std::future::wait
T wait(T...args)
task_with_parameter
void task_with_parameter(int value)
Definition: tutorial.cpp:68
CTest::m_pool
pool m_pool
Definition: tutorial.cpp:96
task_int_23
int task_int_23()
Definition: tutorial.cpp:81
std::to_string
T to_string(T...args)
std
STL namespace.
std::future
boost::threadpool::thread_pool::schedule
bool schedule(task_type const &task)
Schedules a task for asynchronous execution.
Definition: pool.hpp:130
CTest
Definition: tutorial.cpp:94
boost::threadpool::thread_pool::empty
bool empty() const
Indicates that there are no tasks pending.
Definition: pool.hpp:166
boost::threadpool::thread_pool::pending
size_t pending() const
Returns the number of tasks which are ready for execution.
Definition: pool.hpp:148
main
int main(int, char *const [])
Definition: tutorial.cpp:107
compareRootHistos.test
string test
Definition: compareRootHistos.py:15
pool
Definition: PoolClasses.h:7
print
void print(string text)
Definition: tutorial.cpp:32
boost::threadpool::schedule
disable_if< is_void< typename result_of< Function() >::type >, future< typename result_of< Function() >::type >>::type schedule(Pool &pool, const Function &task)
Definition: future.hpp:111
std::ostringstream
STL class.
task_1
void task_1()
Definition: tutorial.cpp:51
std::lock
T lock(T...args)
std::ostringstream::str
T str(T...args)
boost::threadpool::thread_pool::size_controller
size_controller_type size_controller()
Gets the size controller which manages the number of threads in the pool.
Definition: pool.hpp:111
CTest::CTest
CTest()
Definition: tutorial.cpp:98
m_io_monitor
boost::mutex m_io_monitor
Definition: tutorial.cpp:30
boost::threadpool
Definition: future.hpp:32
std::ostringstream::flush
T flush(T...args)
std::future::get
T get(T...args)
boost::threadpool::thread_pool
Thread pool.
Definition: pool.hpp:73
looped_task
bool looped_task()
Definition: tutorial.cpp:74
boost::threadpool::prio_task_func
Prioritized task function object.
Definition: task_adaptors.hpp:51
loops
int loops
Definition: tutorial.cpp:73
std::cout
task_3
void task_3()
Definition: tutorial.cpp:63
task_2
void task_2()
Definition: tutorial.cpp:57
threadpool.hpp
Main include.