-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasioThreadPool.cpp
More file actions
45 lines (37 loc) · 1.01 KB
/
asioThreadPool.cpp
File metadata and controls
45 lines (37 loc) · 1.01 KB
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
#include "asioThreadPool.h"
#include "boost/asio.hpp"
namespace MV{
struct ThreadPoolDetails {
ThreadPoolDetails() :
service(std::make_shared<boost::asio::io_service>()),
working(std::make_unique<boost::asio::io_service::work>(*service)) {
}
std::shared_ptr<boost::asio::io_service> service;
std::unique_ptr<boost::asio::io_service::work> working;
};
AsioThreadPool::AsioThreadPool(size_t a_threads) :
details(std::make_unique<ThreadPoolDetails>()) {
for (size_t i = 0; i < a_threads; ++i) {
workers.emplace_back(std::make_unique<std::thread>([this] {
details->service->run();
}));
}
}
AsioThreadPool::~AsioThreadPool() {
details->service->stop();
for (auto&& worker : workers) {
if (worker->joinable()) {
worker->join();
}
}
}
void AsioThreadPool::task(Job a_newWork) {
details->service->post([=]() mutable {
a_newWork.parent = this;
a_newWork();
});
}
std::shared_ptr<boost::asio::io_service> AsioThreadPool::service() const {
return details->service;
}
}