asyncevent-listenereventtimeoutevent-loopevent-drivenasynchronous-programmingasynctaskintervaltimersasync-taskmultithreadinterval-timertimeout-control
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.3 KiB
72 lines
1.3 KiB
2 months ago
|
#ifndef _ASYNCO_ENGINE_
|
||
|
#define _ASYNCO_ENGINE_
|
||
|
|
||
|
#include <vector>
|
||
|
#include <memory>
|
||
|
using namespace std;
|
||
|
|
||
|
#include <boost/asio.hpp>
|
||
|
|
||
|
namespace marcelb {
|
||
|
namespace asynco {
|
||
|
|
||
|
#define HW_CONCURRENCY_MINIMAL 4
|
||
|
|
||
|
/**
|
||
|
* Internal anonymous class for initializing the ASIO context and thread pool
|
||
|
* !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
|
||
|
*/
|
||
|
class Engine {
|
||
|
public:
|
||
|
boost::asio::io_context io_context;
|
||
|
|
||
|
void run() {
|
||
|
for (auto& runner : runners) {
|
||
|
runner.join();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
|
unique_ptr<boost::asio::io_service::work> work { [&] () {
|
||
|
return new boost::asio::io_service::work(io_context);
|
||
|
} ()};
|
||
|
|
||
|
vector<thread> runners { [&] () {
|
||
|
vector<thread> _runs;
|
||
|
unsigned int num_of_runners;
|
||
|
#ifdef NUM_OF_RUNNERS
|
||
|
num_of_runners = NUM_OF_RUNNERS;
|
||
|
#else
|
||
|
num_of_runners = thread::hardware_concurrency();
|
||
|
if (num_of_runners < HW_CONCURRENCY_MINIMAL) {
|
||
|
num_of_runners = HW_CONCURRENCY_MINIMAL;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
for (int i=0; i<num_of_runners; i++) {
|
||
|
_runs.push_back(thread ( [this] () {
|
||
|
io_context.run();
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
return _runs;
|
||
|
} ()};
|
||
|
|
||
|
};
|
||
|
|
||
|
|
||
|
extern Engine _asynco_engine;
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
|