Add event loop with thread pool
I think it works Signed-off-by: King_DuckZ <king_duckz@gmx.com>
This commit is contained in:
parent
e3b8b563f0
commit
d44529871c
4 changed files with 104 additions and 3 deletions
78
src/evloop.cpp
Normal file
78
src/evloop.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include "evloop.hpp"
|
||||
#include <ev++.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
#include <functional>
|
||||
#include <taskflow/taskflow.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace duck {
|
||||
namespace {
|
||||
auto time_rand = std::bind(std::uniform_int_distribution<int>(2, 8), std::mt19937(std::time(0)));
|
||||
|
||||
class HtmlFetchTimer : public ev::timer {
|
||||
public:
|
||||
HtmlFetchTimer (ev::loop_ref& loop, tf::Subflow* subflow, std::string&& url) :
|
||||
m_subflow(subflow),
|
||||
m_url(std::move(url))
|
||||
{
|
||||
this->set(loop);
|
||||
this->set<HtmlFetchTimer, &HtmlFetchTimer::on_timer>(this);
|
||||
}
|
||||
|
||||
void on_timer() {
|
||||
m_subflow->emplace([this]() {std::cout << "Timer elapsed for " << m_url << "!\n";});
|
||||
}
|
||||
|
||||
void start (double delay) {
|
||||
ev::timer::start(delay, 0.0);
|
||||
}
|
||||
|
||||
private:
|
||||
tf::Subflow* m_subflow;
|
||||
std::string m_url;
|
||||
};
|
||||
|
||||
class EvThreadPool {
|
||||
public:
|
||||
typedef std::thread thread_t;
|
||||
EvThreadPool() :
|
||||
m_loop(ev::AUTO)
|
||||
{
|
||||
}
|
||||
|
||||
void start() {
|
||||
m_taskflow.emplace([this](tf::Subflow& subflow){main_loop(subflow);});
|
||||
m_executor.run(m_taskflow);
|
||||
}
|
||||
|
||||
void join() {
|
||||
m_executor.wait_for_all();
|
||||
std::cout << "all tasks completed\n";
|
||||
}
|
||||
|
||||
private:
|
||||
void main_loop (tf::Subflow& subflow) {
|
||||
m_timer = std::make_unique<HtmlFetchTimer>(m_loop, &subflow, "lalalala");
|
||||
m_timer->start(static_cast<double>(time_rand()));
|
||||
|
||||
this->m_loop.run(0);
|
||||
}
|
||||
|
||||
ev::default_loop m_loop;
|
||||
tf::Taskflow m_taskflow;
|
||||
tf::Executor m_executor;
|
||||
std::unique_ptr<HtmlFetchTimer> m_timer;
|
||||
};
|
||||
} //unnamed namespace
|
||||
|
||||
void test() {
|
||||
//const auto processor_count = std::thread::hardware_concurrency();
|
||||
EvThreadPool worker; //(std::max(2U, processor_count) - 1);
|
||||
worker.start();
|
||||
worker.join();
|
||||
}
|
||||
} //namespace duck
|
5
src/evloop.hpp
Normal file
5
src/evloop.hpp
Normal file
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace duck {
|
||||
void test();
|
||||
} //namespace duck
|
|
@ -9,6 +9,10 @@ sqlitecpp_dep = dependency('sqlitecpp', version: '>=3.0.0',
|
|||
fallback: ['SQLiteCpp', 'sqlitecpp_dep'],
|
||||
)
|
||||
|
||||
ev_dep = dependency('libev', version: '>=4.31')
|
||||
threads_dep = dependency('threads')
|
||||
taskflow_dep = dependency('Cpp-Taskflow', version: '>=2.4.0', method: 'cmake')
|
||||
|
||||
base_url = get_option('base_url').strip()
|
||||
if not base_url.endswith('/')
|
||||
base_url = base_url + '/'
|
||||
|
@ -29,8 +33,15 @@ executable(meson.project_name(),
|
|||
'oro/dateconv.cpp',
|
||||
'oro/items.cpp',
|
||||
'oro/shops.cpp',
|
||||
'evloop.cpp',
|
||||
project_config_file,
|
||||
install: true,
|
||||
dependencies: [restc_cpp_dep, sqlitecpp_dep],
|
||||
dependencies: [
|
||||
restc_cpp_dep,
|
||||
sqlitecpp_dep,
|
||||
ev_dep,
|
||||
threads_dep,
|
||||
taskflow_dep,
|
||||
],
|
||||
include_directories: date_incdir,
|
||||
)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
project('restc-cpp', 'cpp',
|
||||
version: '0.9.2',
|
||||
meson_version: '>=0.49.2',
|
||||
meson_version: '>=0.50.0',
|
||||
default_options: ['cpp_std=gnu++17'],
|
||||
)
|
||||
|
||||
|
@ -17,6 +17,7 @@ boost_dep = dependency('boost',
|
|||
'context', 'coroutine', 'chrono', 'log'
|
||||
]
|
||||
)
|
||||
rapidjson_dep = dependency('RapidJSON')
|
||||
|
||||
if not get_option('embedded_restc_cpp')
|
||||
if get_option('restc_cpp_with_zlib')
|
||||
|
@ -52,7 +53,13 @@ restc_cpp_target = library(meson.project_name(),
|
|||
zipreaderimpl,
|
||||
install: true,
|
||||
include_directories: [pvt_incdir, pub_incdir],
|
||||
dependencies: [zlib_dep, tls_dep, thread_dep, boost_dep],
|
||||
dependencies: [
|
||||
zlib_dep,
|
||||
tls_dep,
|
||||
thread_dep,
|
||||
boost_dep,
|
||||
rapidjson_dep,
|
||||
],
|
||||
cpp_args: compiler_opts,
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue