@@ -17,7 +17,7 @@ class workbranch {
17
17
using worker = autothread<detach>;
18
18
using worker_map = std::map<worker::id, worker>;
19
19
20
- sz_t decline = 0 ;
20
+ sz_t decline = 0 ;
21
21
sz_t task_done_workers = 0 ;
22
22
bool is_waiting = false ;
23
23
bool destructing = false ;
@@ -74,17 +74,17 @@ class workbranch {
74
74
75
75
/* *
76
76
* @brief Wait for all tasks done.
77
- * @brief This interface will pause all threads(workers) in workbranch to relieve system's stress.
78
- * @param timeout timeout for waiting
77
+ * @brief This interface will pause all threads(workers) in workbranch to relieve system's stress.
78
+ * @param timeout timeout for waiting (ms)
79
79
* @return return true if all tasks done
80
80
*/
81
81
bool wait_tasks (unsigned timeout = -1 ) {
82
82
bool res;
83
83
{
84
84
std::unique_lock<std::mutex> locker (lok);
85
- is_waiting = true ;
85
+ is_waiting = true ; // task_done_workers == 0
86
86
res = task_done_cv.wait_for (locker, std::chrono::milliseconds (timeout), [this ]{
87
- return task_done_workers >= workers.size (); // use ">=" to avoid supervisor delete workers
87
+ return task_done_workers >= workers.size (); // use ">=" to avoid supervisor delete workers
88
88
});
89
89
task_done_workers = 0 ;
90
90
is_waiting = false ;
@@ -117,7 +117,7 @@ class workbranch {
117
117
* @return void
118
118
*/
119
119
template <typename T = normal , typename F,
120
- typename R = details::result_of_t <F>,
120
+ typename R = details::result_of_t <F>,
121
121
typename DR = typename std::enable_if<std::is_void<R>::value>::type>
122
122
auto submit (F&& task) -> typename std::enable_if<std::is_same<T, normal>::value>::type {
123
123
tq.push_back ([task]{
@@ -128,7 +128,7 @@ class workbranch {
128
128
} catch (...) {
129
129
std::cerr<<" workspace: worker[" << std::this_thread::get_id ()<<" ] caught unknown exception\n " <<std::flush;
130
130
}
131
- });
131
+ }); // function
132
132
}
133
133
134
134
/* *
@@ -154,6 +154,7 @@ class workbranch {
154
154
/* *
155
155
* @brief async execute tasks
156
156
* @param task runnable object (sequence)
157
+ * @param tasks other parameters
157
158
* @return void
158
159
*/
159
160
template <typename T, typename F, typename ... Fs>
@@ -172,7 +173,6 @@ class workbranch {
172
173
/* *
173
174
* @brief async execute the task
174
175
* @param task runnable object (normal)
175
- * @param dummy dummy
176
176
* @return std::future<R>
177
177
*/
178
178
template <typename T = normal , typename F,
@@ -200,7 +200,6 @@ class workbranch {
200
200
/* *
201
201
* @brief async execute the task
202
202
* @param task runnable object (urgent)
203
- * @param dummy dummy
204
203
* @return std::future<R>
205
204
*/
206
205
template <typename T, typename F,
@@ -226,16 +225,16 @@ class workbranch {
226
225
}
227
226
228
227
private:
229
- // loop
228
+ // thread's default loop
230
229
void mission () {
231
230
std::function<void ()> task;
232
231
while (true ) {
233
232
if (decline <= 0 && tq.try_pop (task)) {
234
233
task ();
235
234
} else if (decline > 0 ) {
236
235
std::lock_guard<std::mutex> lock (lok);
237
- if (decline > 0 && decline--) {
238
- workers.erase (std::this_thread::get_id ());
236
+ if (decline > 0 && decline--) { // double check
237
+ workers.erase (std::this_thread::get_id ());
239
238
if (is_waiting)
240
239
task_done_cv.notify_one ();
241
240
if (destructing)
@@ -247,9 +246,9 @@ class workbranch {
247
246
std::unique_lock<std::mutex> locker (lok);
248
247
task_done_workers++;
249
248
task_done_cv.notify_one ();
250
- thread_cv.wait (locker);
249
+ thread_cv.wait (locker);
251
250
} else {
252
- std::this_thread::yield ();
251
+ std::this_thread::yield ();
253
252
}
254
253
}
255
254
}
0 commit comments