All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
compile_all.cpp
Go to the documentation of this file.
1 
19 #include <iostream>
20 #include <sstream>
21 #include <boost/thread/mutex.hpp>
22 #include <boost/bind.hpp>
23 
24 #include <boost/threadpool.hpp>
25 
26 using namespace std;
27 using namespace boost::threadpool;
28 
29 
30 //
31 // Helpers
32 boost::mutex m_io_monitor;
33 
34 void print(string text)
35 {
36  boost::mutex::scoped_lock lock(m_io_monitor);
37  cout << text;
38 }
39 
40 template<typename T>
41 string to_string(T const & value)
42 {
43  ostringstream ost;
44  ost << value;
45  ost.flush();
46  return ost.str();
47 }
48 
49 
50 
51 //
52 // An example task functions
53 void task_1()
54 {
55  print(" task_1()\n");
56 }
57 
58 void task_2()
59 {
60  print(" task_2()\n");
61 }
62 
63 void task_3()
64 {
65  print(" task_3()\n");
66 }
67 
68 int task_4()
69 {
70  print(" task_4()\n");
71  return 4;
72 }
73 
74 void task_with_parameter(int value)
75 {
76  print(" task_with_parameter(" + to_string(value) + ")\n");
77 }
78 
79 int loops = 0;
81 {
82  print(" looped_task()\n");
83  return ++loops < 5;
84 }
85 
86 
87 int task_int()
88 {
89  print(" task_int()\n");
90  return 23;
91 }
92 
93 
95 {
96  pool tp;
97 
98  tp.schedule(&task_1);
99  tp.schedule(boost::bind(task_with_parameter, 4));
100 
101  if(!tp.empty())
102  {
103  tp.clear(); // remove all tasks -> no output in this test
104  }
105 
106  size_t active_threads = tp.active();
107  size_t pending_threads = tp.pending();
108  size_t total_threads = tp.size();
109 
110  size_t dummy = active_threads + pending_threads + total_threads;
111  dummy++;
112 
113  tp.size_controller().resize(5);
114  tp.wait();
115 }
116 
118 {
119  lifo_pool tp;
120  tp.size_controller().resize(0);
121  schedule(tp, &task_1);
122  tp.size_controller().resize(10);
123  tp.wait();
124 }
125 
127 {
128  prio_pool tp(2);
129  schedule(tp, prio_task_func(1, &task_1));
130  schedule(tp, prio_task_func(10,&task_2));
131 }
132 
133 
135 {
136  fifo_pool tp(5);
137  future<int> fut = schedule(tp, &task_4);
138  int res = fut();
139 }
140 
141 
142 int main (int , char * const [])
143 {
144  fifo_pool_test();
145  lifo_pool_test();
146  prio_pool_test();
147  future_test();
148  return 0;
149 }
void lifo_pool_test()
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:80
void task_with_parameter(int value)
Definition: compile_all.cpp:74
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:63
bool empty() const
Indicates that there are no tasks pending.
Definition: pool.hpp:166
int loops
Definition: compile_all.cpp:79
int task_4()
Definition: compile_all.cpp:68
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:53
T flush(T...args)
void print(string text)
Definition: compile_all.cpp:34
void task_2()
Definition: compile_all.cpp:58
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:32
int task_int()
Definition: compile_all.cpp:87
void fifo_pool_test()
Definition: compile_all.cpp:94
size_t size() const
Gets the number of threads in the pool.
Definition: pool.hpp:120
Main include.