-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathioservicehandler.cpp
More file actions
50 lines (44 loc) · 1.33 KB
/
Copy pathioservicehandler.cpp
File metadata and controls
50 lines (44 loc) · 1.33 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
46
47
48
49
50
#include "ioservicehandler.h"
IOServiceHandler::IOServiceHandler(std::size_t _numThreads)
: ptrIo(new boost::asio::io_service), numThreads(_numThreads)
{
LOG(__PRETTY_FUNCTION__, " numThreads ", numThreads);
start();
}
void IOServiceHandler::start()
{
LOG(__PRETTY_FUNCTION__);
// give some work to the io_service to prevent run() from returning
work.reset(new boost::asio::io_service::work(*ptrIo));
//create threads calling run()
for(std::size_t i = 0; i < numThreads; i++){
threadPool.push_back(std::thread/*(boost::bind*/(&IOServiceHandler::runThread, this))/*)*/;
}
}
void IOServiceHandler::runThread()
{
LOG(__PRETTY_FUNCTION__, " ", std::this_thread::get_id());
ptrIo->run();
}
void IOServiceHandler::stop()
{
LOG(__PRETTY_FUNCTION__);
// Reset work so io_Service cal stop
work.reset();
// join all threads
for(std::vector<std::thread>::iterator iter = threadPool.begin(); iter != threadPool.end(); iter++){
LOG(__PRETTY_FUNCTION__," joined thread ",iter->get_id());
iter->join();
}
threadPool.erase(threadPool.begin(),threadPool.end());
}
std::shared_ptr<boost::asio::io_service> IOServiceHandler::getIoService()
{
LOG(__PRETTY_FUNCTION__);
return ptrIo;
}
IOServiceHandler::~IOServiceHandler()
{
LOG(__PRETTY_FUNCTION__);
stop();
}