-
Notifications
You must be signed in to change notification settings - Fork 1
/
workers.cpp
51 lines (42 loc) · 1.11 KB
/
workers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "workers.hpp"
#include "trace.hpp"
namespace workers {
workers::workers(int num_workers):
m_num_workers(num_workers)
{
m_work = std::make_shared<boost::asio::io_context::work>(m_io_context);
m_worker_threads = std::make_shared<boost::thread_group>();
for (int i = 0; i != m_num_workers; ++i) {
m_worker_threads->create_thread(boost::bind(&workers::worker, this, std::ref(m_io_context)));
}
}
boost::asio::io_context &workers::io_context()
{
return m_io_context;
}
void workers::stop()
{
m_running = false;
m_io_context.stop();
m_worker_threads->join_all();
}
int workers::worker(boost::asio::io_context &io_context)
{
while (m_running) {
try {
boost::system::error_code ec;
io_context.run(ec);
if (ec) {
TRACE(L"Error:" << ec.message().c_str());
}
}
catch (std::exception &ex) {
TRACE("Exception '" << ex.what() << "'");
}
catch (boost::system::error_code &ec) {
TRACE("Exception: " << ec.message().c_str());
}
}
return 0;
}
}