| 1 |
#include "clConcurrent.hpp" |
| 2 |
|
| 3 |
#include "file_logger.h" |
| 4 |
|
| 5 |
#include <sstream> |
| 6 |
|
| 7 |
clConcurrent::clConcurrent(size_t pool_size) |
| 8 |
: m_pool_size(pool_size) |
| 9 |
{ |
| 10 |
m_shutdown.store(false); |
| 11 |
} |
| 12 |
|
| 13 |
clConcurrent::~clConcurrent() { shutdown(); } |
| 14 |
|
| 15 |
void clConcurrent::run() |
| 16 |
{ |
| 17 |
shutdown(); |
| 18 |
m_thread_pool.reserve(m_pool_size); |
| 19 |
|
| 20 |
for(size_t i = 0; i < m_pool_size; ++i) { |
| 21 |
std::thread* thr = new std::thread([&]() { |
| 22 |
std::stringstream ss; |
| 23 |
ss << std::this_thread::get_id(); |
| 24 |
clDEBUG() << "worker thread #" << ss.str() << endl; |
| 25 |
Callback command; |
| 26 |
while(!m_shutdown.load()) { |
| 27 |
Callback cb; |
| 28 |
if(m_queue.ReceiveTimeout(100, cb) == wxMSGQUEUE_NO_ERROR) { |
| 29 |
cb(); |
| 30 |
} |
| 31 |
} |
| 32 |
clDEBUG() << "worker thread #" << ss.str() << "is going down" << endl; |
| 33 |
}); |
| 34 |
m_thread_pool.push_back(thr); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
void clConcurrent::shutdown() |
| 39 |
{ |
| 40 |
m_shutdown.store(true); |
| 41 |
for(auto thr : m_thread_pool) { |
| 42 |
thr->join(); |
| 43 |
wxDELETE(thr); |
| 44 |
} |
| 45 |
m_thread_pool.clear(); |
| 46 |
m_shutdown.store(false); |
| 47 |
} |