The Gaudi Framework  v29r0 (ff2e7097)
compile_all.cpp
Go to the documentation of this file.
1 
16 #include <boost/bind.hpp>
17 #include <boost/thread/mutex.hpp>
18 #include <iostream>
19 #include <sstream>
20 
21 #include <boost/threadpool.hpp>
22 
23 using namespace std;
24 using namespace boost::threadpool;
25 
26 //
27 // Helpers
28 boost::mutex m_io_monitor;
29 
30 void print( string text )
31 {
32  boost::mutex::scoped_lock lock( m_io_monitor );
33  cout << text;
34 }
35 
36 template <typename T>
37 string to_string( T const& value )
38 {
39  ostringstream ost;
40  ost << value;
41  ost.flush();
42  return ost.str();
43 }
44 
45 //
46 // An example task functions
47 void task_1() { print( " task_1()\n" ); }
48 
49 void task_2() { print( " task_2()\n" ); }
50 
51 void task_3() { print( " task_3()\n" ); }
52 
53 int task_4()
54 {
55  print( " task_4()\n" );
56  return 4;
57 }
58 
59 void task_with_parameter( int value ) { print( " task_with_parameter(" + to_string( value ) + ")\n" ); }
60 
61 int loops = 0;
63 {
64  print( " looped_task()\n" );
65  return ++loops < 5;
66 }
67 
68 int task_int()
69 {
70  print( " task_int()\n" );
71  return 23;
72 }
73 
75 {
76  pool tp;
77 
78  tp.schedule( &task_1 );
79  tp.schedule( boost::bind( task_with_parameter, 4 ) );
80 
81  if ( !tp.empty() ) {
82  tp.clear(); // remove all tasks -> no output in this test
83  }
84 
85  size_t active_threads = tp.active();
86  size_t pending_threads = tp.pending();
87  size_t total_threads = tp.size();
88 
89  size_t dummy = active_threads + pending_threads + total_threads;
90  dummy++;
91 
92  tp.size_controller().resize( 5 );
93  tp.wait();
94 }
95 
97 {
98  lifo_pool tp;
99  tp.size_controller().resize( 0 );
100  schedule( tp, &task_1 );
101  tp.size_controller().resize( 10 );
102  tp.wait();
103 }
104 
106 {
107  prio_pool tp( 2 );
108  schedule( tp, prio_task_func( 1, &task_1 ) );
109  schedule( tp, prio_task_func( 10, &task_2 ) );
110 }
111 
113 {
114  fifo_pool tp( 5 );
115  future<int> fut = schedule( tp, &task_4 );
116  int res = fut();
117 }
118 
119 int main( int, char* const[] )
120 {
121  fifo_pool_test();
122  lifo_pool_test();
123  prio_pool_test();
124  future_test();
125  return 0;
126 }
void lifo_pool_test()
Definition: compile_all.cpp:96
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
bool looped_task()
Definition: compile_all.cpp:62
void task_with_parameter(int value)
Definition: compile_all.cpp:59
T to_string(T...args)
STL namespace.
void future_test()
bool schedule(task_type const &task)
Schedules a task for asynchronous execution.
Definition: pool.hpp:130
void task_3()
Definition: compile_all.cpp:51
bool empty() const
Indicates that there are no tasks pending.
Definition: pool.hpp:166
int loops
Definition: compile_all.cpp:61
int task_4()
Definition: compile_all.cpp:53
size_t pending() const
Returns the number of tasks which are ready for execution.
Definition: pool.hpp:148
void prio_pool_test()
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
T lock(T...args)
size_t active() const
Returns the number of tasks which are currently executed.
Definition: pool.hpp:139
size_controller_type size_controller()
Gets the size controller which manages the number of threads in the pool.
Definition: pool.hpp:111
int main(int, char *const [])
void task_1()
Definition: compile_all.cpp:47
T flush(T...args)
void print(string text)
Definition: compile_all.cpp:30
void task_2()
Definition: compile_all.cpp:49
void clear()
Removes all pending tasks from the pool&#39;s scheduler.
Definition: pool.hpp:156
Prioritized task function object.
boost::mutex m_io_monitor
Definition: compile_all.cpp:28
int task_int()
Definition: compile_all.cpp:68
void fifo_pool_test()
Definition: compile_all.cpp:74
size_t size() const
Gets the number of threads in the pool.
Definition: pool.hpp:120
Main include.