| The Gaudi Framework v28r2-patches (bc4ad17a)

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

- File List
- File Members

Classes | Functions | Variables
tutorial.cpp File Reference

threadpool tutorial. More...

`#include <stdlib.h>`
`#include <iostream>`
`#include <sstream>`
`#include <boost/thread/mutex.hpp>`
`#include <boost/bind.hpp>`
`#include <boost/threadpool.hpp>`
Include dependency graph for tutorial.cpp:

Go to the source code of this file.

## Classes
class | CTest

## Functions
void | print (string text)
template<typename T >
string | to_string (T const &value)
void | task_1 ()
void | task_2 ()
void | task_3 ()
void | task_with_parameter (int value)
bool | looped_task ()
int | task_int_23 ()
int | task_int_1 ()
int | main (int, char *const [])

## Variables
boost::mutex | m_io_monitor
int | loops = 0

## Detailed Description

threadpool tutorial.

This file contains a tutorial for the threadpool library.

Copyright (c) 2005-2007 Philipp Henkel

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

http://threadpool.sourceforge.net

Definition in file tutorial.cpp.

## Function Documentation

bool looped_task | ( |  | ) |

Definition at line 74 of file tutorial.cpp.

75 {
76 print(" looped_task()\n");
77 return ++loops < 5;
78 }
print
void print(string text)
Definition: tutorial.cpp:32
loops
int loops
Definition: tutorial.cpp:73

int main | ( | int | ,
|  | char * | const[]
| ) |  |

Definition at line 107 of file tutorial.cpp.

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
task_int_23
int task_int_23()
Definition: tutorial.cpp:81
std::future
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
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
task_1
void task_1()
Definition: tutorial.cpp:51
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
std::future::get
T get(T...args)
boost::threadpool::thread_pool
Thread pool.
Definition: pool.hpp:73
boost::threadpool::prio_task_func
Prioritized task function object.
Definition: task_adaptors.hpp:51
task_3
void task_3()
Definition: tutorial.cpp:63
task_2
void task_2()
Definition: tutorial.cpp:57

void print | ( | string | text | ) |

Definition at line 32 of file tutorial.cpp.

33 {
34 boost::mutex::scoped_lock lock(m_io_monitor);
35 cout << text;
36 }
std::lock
T lock(T...args)
m_io_monitor
boost::mutex m_io_monitor
Definition: tutorial.cpp:30
std::cout

void task_1 | ( |  | ) |

Definition at line 51 of file tutorial.cpp.

52 {
53 print(" task_1()\n");
54 throw 5;
55 }
print
void print(string text)
Definition: tutorial.cpp:32

void task_2 | ( |  | ) |

Definition at line 57 of file tutorial.cpp.

58 {
59 print(" task_2()\n");
60 throw 5;
61 }
print
void print(string text)
Definition: tutorial.cpp:32

void task_3 | ( |  | ) |

Definition at line 63 of file tutorial.cpp.

64 {
65 print(" task_3()\n");
66 }
print
void print(string text)
Definition: tutorial.cpp:32

int task_int_1 | ( |  | ) |

Definition at line 87 of file tutorial.cpp.

88 {
89 print(" task_int_1()\n");
90 return 1;
91 }
print
void print(string text)
Definition: tutorial.cpp:32

int task_int_23 | ( |  | ) |

Definition at line 81 of file tutorial.cpp.

82 {
83 print(" task_int_23()\n");
84 return 23;
85 }
print
void print(string text)
Definition: tutorial.cpp:32

void task_with_parameter | ( | int | value | ) |

Definition at line 68 of file tutorial.cpp.

69 {
70 print(" task_with_parameter(" + to_string(value) + ")\n");
71 }
print
void print(string text)
Definition: tutorial.cpp:32
to_string
string to_string(T const &value)
Definition: tutorial.cpp:39
template<typename T >

string to_string | ( | T const & | value | ) |

Definition at line 39 of file tutorial.cpp.

40 {
41 ostringstream ost;
42 ost << value;
43 ost.flush();
44 return ost.str();
45 }
std::ostringstream
STL class.
std::ostringstream::str
T str(T...args)
std::ostringstream::flush
T flush(T...args)

## Variable Documentation

int loops = 0

Definition at line 73 of file tutorial.cpp.

boost::mutex m_io_monitor

Definition at line 30 of file tutorial.cpp.