| The Gaudi Framework v30r0 (c919700c)

- 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 <boost/bind.hpp>`
`#include <boost/thread/mutex.hpp>`
`#include <boost/threadpool.hpp>`
`#include <iostream>`
`#include <sstream>`
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 66 of file tutorial.cpp.

67 {
68 print( " looped_task()\n" );
69 return ++loops < 5;
70 }
print
void print(string text)
Definition: tutorial.cpp:32
loops
int loops
Definition: tutorial.cpp:65

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

Definition at line 94 of file tutorial.cpp.

95 {
96 print( "\nWelcome to the threadpool tutorial!\n" );
97
98 print( "\n**************************************\n" );
99 print( "Section 1: Quick Start\n" );
100
101 // void func()
102 {
103 print( " Create a new thread pool\n" );
104 pool tp( 2 ); // tp is handle to the pool
105
106 // Add tasks
107 tp.schedule( &task_1 );
108 tp.schedule( &task_2 );
109 tp.schedule( &task_3 );
110 tp.schedule( boost::bind( task_with_parameter, 4 ) );
111
112 // The pool handle tp is allocated on stack and will
113 // be destructed if it gets out of scope. Before the
114 // pool is destroyed it waits for its tasks.
115 // That means the current thread of execution is
116 // blocked at the end of the function
117 // (until all tasks are processed).
118 // while (&tp){int i = 3; ++i;}
119 }
120
121 { // Section Futures
122 print( "\n**************************************\n" );
123 print( "Section 1: Futures\n" );
124
125 // typedef thread_pool<task_func, fifo_scheduler, static_size, empty_controller, wait_for_all_tasks> test_pool;
126
127 pool tp;
128
129 // tp.resize(0);
130 tp.pending();
131 // tp.clear();
132 boost::xtime t;
133 tp.wait( t );
134 bool test = tp.empty();
135 if ( test ) {
136 test = false;
137 }
138
139 tp.size_controller().resize( 2 );
140
141 // test_pool::size_controller_type controller = tp.size_controller();
142 // controller.resize(5);
143
144 schedule( tp, &task_int_1 );
145 future<int> res = schedule( tp, &task_int_23 );
146 future<int> res2 = schedule( tp, &task_int_1 );
147
148 res.wait();
149 int value = res.get() + res2.get();
150
151 res.cancel();
152 res.is_cancelled();
153 value++;
154
155 // thread_pool<boost::function0<int>, fifo_scheduler> test2332;
156
157 // TODO runnable comile test
158 }
159
160 { // Section 2
161 print( "\n**************************************\n" );
162 print( "Section 2: Controlling scheduling\n" );
163
164 // Create a lifo_pool: last task in, first task out
165 lifo_pool tp( 0 );
166
167 print( " Add tasks (using the pool's schedule function)\n" );
168 schedule( tp, &task_1 );
169 schedule( tp, &task_2 );
170 schedule( tp, &task_3 );
171
172 // tp.wait(); This would be a deadlock as there are no threads which process the tasks.
173
174 print( " Add some threads ...\n" );
175 tp.size_controller().resize( 5 );
176
177 print( " Wait until all tasks are finished ...\n" );
178 tp.wait();
179 print( " Tasks finished!\n" );
180 }
181
182 { // Section 3:
183 print( "\n**************************************\n" );
184 print( "Section 3: Prioritized Tasks\n" );
185
186 prio_pool tp( 0 );
187
188 print( " Add prioritized tasks ...\n" );
189 schedule( tp, prio_task_func( 1, &task_1 ) );
190 schedule( tp, prio_task_func( 10, &task_2 ) );
191 schedule( tp, prio_task_func( 5, &task_3 ) );
192
193 // Tasks are ordered according to their priority: task_2, task_4, task_3, task_1
194
195 print( " Thread added\n" );
196 tp.size_controller().resize( 10 );
197
198 print( " Wait until all tasks are finished ...\n" );
199 tp.wait();
200 print( " Tasks finished!\n" );
201 }
202
203 /* */
204 { // Section 5:
205 print( "\n**************************************\n" );
206 print( "Section 5: Advanced thread pool instantiation\n" );
207 // Create the pool directly
208 /*
209 TODO
210 boost::shared_ptr<fifo_pool> tp = fifo_pool::create_pool(5);
211
212 print(" Add tasks ...\n");
213 tp->schedule(&task_1);
214 tp->schedule(&task_2);
215 tp->schedule(&task_3);
216 tp->schedule(looped_task_func(&looped_task, 1500));
217
218 print(" Wait until all tasks are finished ...\n");
219 tp->wait();
220 */
221
222 print( " Tasks finished!\n" );
223 }
224
225 print( "\n**************************************\n" );
226 print( "Tutorial finished!\n" );
227
228 { // Section Compile Tests
229 print( "\n**************************************\n" );
230 print( "Section Compile Tests\n" );
231
232 fifo_pool tp;
233 tp.size_controller().resize( 0 );
234 tp.empty();
235 }
236
237 return 0;
238 }
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:78
std::future::wait
T wait(T...args)
task_with_parameter
void task_with_parameter(int value)
Definition: tutorial.cpp:63
task_int_23
int task_int_23()
Definition: tutorial.cpp:72
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:11
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:49
compareRootHistos.tp
tuple tp
Definition: compareRootHistos.py:417
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:61
task_2
void task_2()
Definition: tutorial.cpp:55

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 49 of file tutorial.cpp.

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

void task_2 | ( |  | ) |

Definition at line 55 of file tutorial.cpp.

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

void task_3 | ( |  | ) |

Definition at line 61 of file tutorial.cpp.

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

int task_int_1 | ( |  | ) |

Definition at line 78 of file tutorial.cpp.

79 {
80 print( " task_int_1()\n" );
81 return 1;
82 }
print
void print(string text)
Definition: tutorial.cpp:32

int task_int_23 | ( |  | ) |

Definition at line 72 of file tutorial.cpp.

73 {
74 print( " task_int_23()\n" );
75 return 23;
76 }
print
void print(string text)
Definition: tutorial.cpp:32

void task_with_parameter | ( | int | value | ) |

Definition at line 63 of file tutorial.cpp.

63 { print( " task_with_parameter(" + to_string( value ) + ")\n" ); }
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 65 of file tutorial.cpp.

boost::mutex m_io_monitor

Definition at line 30 of file tutorial.cpp.