|
a/libupnpp/workqueue.h |
|
b/libupnpp/workqueue.h |
|
... |
|
... |
16 |
*/
|
16 |
*/
|
17 |
#ifndef _WORKQUEUE_H_INCLUDED_
|
17 |
#ifndef _WORKQUEUE_H_INCLUDED_
|
18 |
#define _WORKQUEUE_H_INCLUDED_
|
18 |
#define _WORKQUEUE_H_INCLUDED_
|
19 |
|
19 |
|
20 |
#include <thread>
|
20 |
#include <thread>
|
|
|
21 |
#if HAVE_STD_FUTURE
|
21 |
#include <future>
|
22 |
#include <future>
|
|
|
23 |
#endif
|
22 |
#include <string>
|
24 |
#include <string>
|
23 |
#include <queue>
|
25 |
#include <queue>
|
24 |
#include <list>
|
26 |
#include <list>
|
25 |
#include <mutex>
|
27 |
#include <mutex>
|
26 |
#include <condition_variable>
|
28 |
#include <condition_variable>
|
|
... |
|
... |
74 |
* @return true if ok.
|
76 |
* @return true if ok.
|
75 |
*/
|
77 |
*/
|
76 |
bool start(int nworkers, void *(workproc)(void *), void *arg) {
|
78 |
bool start(int nworkers, void *(workproc)(void *), void *arg) {
|
77 |
std::unique_lock<std::mutex> lock(m_mutex);
|
79 |
std::unique_lock<std::mutex> lock(m_mutex);
|
78 |
for (int i = 0; i < nworkers; i++) {
|
80 |
for (int i = 0; i < nworkers; i++) {
|
|
|
81 |
Worker w;
|
|
|
82 |
#if HAVE_STD_FUTURE
|
79 |
std::packaged_task<void *(void *)> task(workproc);
|
83 |
std::packaged_task<void *(void *)> task(workproc);
|
80 |
Worker w;
|
|
|
81 |
w.res = task.get_future();
|
84 |
w.res = task.get_future();
|
82 |
w.thr = std::thread(std::move(task), arg);
|
85 |
w.thr = std::thread(std::move(task), arg);
|
|
|
86 |
#else
|
|
|
87 |
w.thr = std::thread(workproc, arg);
|
|
|
88 |
#endif
|
83 |
m_worker_threads.push_back(std::move(w));
|
89 |
m_worker_threads.push_back(std::move(w));
|
84 |
}
|
90 |
}
|
85 |
return true;
|
91 |
return true;
|
86 |
}
|
92 |
}
|
87 |
|
93 |
|
|
... |
|
... |
187 |
m_clientsleeps << "\n");
|
193 |
m_clientsleeps << "\n");
|
188 |
// Perform the thread joins and compute overall status
|
194 |
// Perform the thread joins and compute overall status
|
189 |
// Workers return (void*)1 if ok
|
195 |
// Workers return (void*)1 if ok
|
190 |
void *statusall = (void*)1;
|
196 |
void *statusall = (void*)1;
|
191 |
while (!m_worker_threads.empty()) {
|
197 |
while (!m_worker_threads.empty()) {
|
|
|
198 |
#if HAVE_STD_FUTURE
|
192 |
void *status = m_worker_threads.front().res.get();
|
199 |
void *status = m_worker_threads.front().res.get();
|
|
|
200 |
#else
|
|
|
201 |
void *status = (void*) 1;
|
|
|
202 |
#endif
|
193 |
m_worker_threads.front().thr.join();
|
203 |
m_worker_threads.front().thr.join();
|
194 |
if (status == (void *)0) {
|
204 |
if (status == (void *)0) {
|
195 |
statusall = status;
|
205 |
statusall = status;
|
196 |
}
|
206 |
}
|
197 |
m_worker_threads.pop_front();
|
207 |
m_worker_threads.pop_front();
|
|
... |
|
... |
303 |
return isok;
|
313 |
return isok;
|
304 |
}
|
314 |
}
|
305 |
|
315 |
|
306 |
struct Worker {
|
316 |
struct Worker {
|
307 |
std::thread thr;
|
317 |
std::thread thr;
|
|
|
318 |
#if HAVE_STD_FUTURE
|
308 |
std::future<void *> res;
|
319 |
std::future<void *> res;
|
|
|
320 |
#endif
|
309 |
};
|
321 |
};
|
310 |
|
322 |
|
311 |
// Configuration
|
323 |
// Configuration
|
312 |
std::string m_name;
|
324 |
std::string m_name;
|
313 |
size_t m_high;
|
325 |
size_t m_high;
|