| The Gaudi Framework v29r0 (ff2e7097)

- 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 <boost/bind.hpp>
20 #include <boost/thread/mutex.hpp>
21 #include <boost/threadpool.hpp>
22 #include <iostream>
23 #include <sstream>
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 // An example task functions
49 void task_1()
50 {
51 print( " task_1()\n" );
52 throw 5;
53 }
54
55 void task_2()
56 {
57 print( " task_2()\n" );
58 throw 5;
59 }
60
61 void task_3() { print( " task_3()\n" ); }
62
63 void task_with_parameter( int value ) { print( " task_with_parameter(" + to_string( value ) + ")\n" ); }
64
65 int loops = 0;
66 bool looped_task()
67 {
68 print( " looped_task()\n" );
69 return ++loops < 5;
70 }
71
72 int task_int_23()
73 {
74 print( " task_int_23()\n" );
75 return 23;
76 }
77
78 int task_int_1()
79 {
80 print( " task_int_1()\n" );
81 return 1;
82 }
83
84 class CTest
85 {
86 pool m_pool;
87
88 public:
89 CTest() : m_pool( pool( 1000 ) ) {}
90 };
91
92 //
93 // A demonstration of the thread_pool class
94 int main( int, char* const[] )
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
CTest::m_pool
pool m_pool
Definition: tutorial.cpp:86
task_int_23
int task_int_23()
Definition: tutorial.cpp:72
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:84
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:94
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
std::ostringstream
STL class.
task_1
void task_1()
Definition: tutorial.cpp:49
compareRootHistos.tp
tuple tp
Definition: compareRootHistos.py:415
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:89
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:66
boost::threadpool::prio_task_func
Prioritized task function object.
Definition: task_adaptors.hpp:51
loops
int loops
Definition: tutorial.cpp:65
std::cout
task_3
void task_3()
Definition: tutorial.cpp:61
task_2
void task_2()
Definition: tutorial.cpp:55
threadpool.hpp
Main include.