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.
113 lines
2.7 KiB
113 lines
2.7 KiB
8 months ago
|
#ifndef _RUNNER_
|
||
|
#define _RUNNER_
|
||
8 months ago
|
|
||
|
#include <thread>
|
||
|
#include <vector>
|
||
|
#include <queue>
|
||
|
#include <functional>
|
||
|
#include <mutex>
|
||
|
#include <condition_variable>
|
||
|
#include <future>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
namespace marcelb {
|
||
|
|
||
8 months ago
|
#ifdef ON_RUNNER
|
||
|
extern runner on_async;
|
||
8 months ago
|
#endif
|
||
|
|
||
8 months ago
|
/**
|
||
|
* The runner class implements multithread, task stack and event loop for asynchronous execution of tasks
|
||
|
*/
|
||
|
class runner {
|
||
8 months ago
|
private:
|
||
8 months ago
|
vector<thread> runners;
|
||
8 months ago
|
queue<function<void()>> tasks;
|
||
|
mutex q_io;
|
||
|
condition_variable cv;
|
||
|
bool stop;
|
||
|
|
||
|
public:
|
||
8 months ago
|
|
||
|
/**
|
||
|
* The constructor starts as many threads as the system has cores,
|
||
|
* and runs an event loop inside each one.
|
||
|
* Each event loop waits for tasks from the stack and executes them.
|
||
|
*/
|
||
|
runner(size_t pool_size = thread::hardware_concurrency()) : stop(false) {
|
||
8 months ago
|
for (size_t i = 0; i < pool_size; ++i) {
|
||
8 months ago
|
runners.emplace_back( thread([this] {
|
||
8 months ago
|
while (true) {
|
||
|
function<void()> task;
|
||
|
{
|
||
|
unique_lock<mutex> lock(q_io);
|
||
|
cv.wait(lock, [this] { return stop || !tasks.empty(); });
|
||
|
if (stop && tasks.empty())
|
||
|
return;
|
||
|
task = move(tasks.front());
|
||
|
tasks.pop();
|
||
|
}
|
||
|
task();
|
||
|
}
|
||
8 months ago
|
}));
|
||
8 months ago
|
}
|
||
|
}
|
||
|
|
||
8 months ago
|
/**
|
||
|
* With the method, we send the callback function and its arguments to the task stack
|
||
|
*/
|
||
8 months ago
|
template<class F, class... Args>
|
||
|
auto put_task(F&& f, Args&&... args)
|
||
|
-> future<typename result_of<F(Args...)>::type> {
|
||
|
using return_type = typename result_of<F(Args...)>::type;
|
||
|
|
||
|
auto task = make_shared<packaged_task<return_type()>>(bind(forward<F>(f), forward<Args>(args)...));
|
||
|
future<return_type> res = task->get_future();
|
||
|
{
|
||
|
unique_lock<mutex> lock(q_io);
|
||
|
|
||
|
if (stop) {
|
||
|
throw runtime_error("Pool is stoped!");
|
||
|
}
|
||
|
|
||
|
tasks.emplace([task]() { (*task)(); });
|
||
|
}
|
||
|
|
||
|
cv.notify_one();
|
||
|
return res;
|
||
|
}
|
||
|
|
||
8 months ago
|
/**
|
||
|
* Returns the number of tasks the runner has to perform
|
||
|
*/
|
||
8 months ago
|
unsigned int count_tasks() {
|
||
|
return tasks.size();
|
||
|
}
|
||
|
|
||
8 months ago
|
/**
|
||
|
* Returns the number of threads used by the runner
|
||
|
*/
|
||
8 months ago
|
unsigned int count_threads() {
|
||
8 months ago
|
return runners.size();
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
/**
|
||
|
* The destructor stops all loops and stops all threads
|
||
|
*/
|
||
|
~runner() {
|
||
8 months ago
|
{
|
||
|
unique_lock<mutex> lock(q_io);
|
||
|
stop = true;
|
||
|
}
|
||
|
cv.notify_all();
|
||
8 months ago
|
for (thread& runner : runners) {
|
||
|
runner.join();
|
||
8 months ago
|
}
|
||
8 months ago
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
#endif
|