deadline_timers的非阻塞升压io_service
Non blocking boost io_service for deadline_timers
在阅读了boost::asio::deadline_timer的文档后,io_service::run()和处理程序方法似乎是在同一个线程上调用的。在后台线程上运行io_service对象时,是否有任何方法可以在一个线程上创建计时器?
为了好玩和荣耀,下面是如何将线程队列与asio截止日期计时器结合起来,从截止日期计时器中调度非阻塞任务:
在Coliru上直播
#ifndef HEADER_GUARD_CUSTOM_THREADPOOL_HPP
#define HEADER_GUARD_CUSTOM_THREADPOOL_HPP
#include <boost/function.hpp>
#include <boost/optional.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>
#include <boost/phoenix.hpp>
#include <boost/make_shared.hpp>
#include <iostream>
#include <string>
#include <deque>
namespace custom {
using namespace boost;
class thread_pool
{
private:
mutex mx;
condition_variable cv;
typedef function<void()> job_t;
std::deque<job_t> _queue;
thread_group pool;
boost::atomic_bool shutdown;
static void worker_thread(thread_pool& q)
{
while (optional<job_t> job = q.dequeue())
(*job)();
}
public:
thread_pool() : shutdown(false) {
//LOG_INFO_MESSAGE << "Number of possible Threads: " << boost::thread::hardware_concurrency() << std::endl;
for (unsigned i = 0; i < boost::thread::hardware_concurrency(); ++i){
pool.create_thread(bind(worker_thread, ref(*this)));
}
}
void enqueue(job_t job)
{
lock_guard<mutex> lk(mx);
_queue.push_back(job);
cv.notify_one();
}
optional<job_t> dequeue()
{
unique_lock<mutex> lk(mx);
namespace phx = boost::phoenix;
cv.wait(lk, phx::ref(shutdown) || !phx::empty(phx::ref(_queue)));
if (_queue.empty())
return none;
job_t job = _queue.front();
_queue.pop_front();
return job;
}
~thread_pool()
{
shutdown = true;
{
lock_guard<mutex> lk(mx);
cv.notify_all();
}
pool.join_all();
}
};
}
#endif // HEADER_GUARD_CUSTOM_THREADPOOL_HPP
和简单的测试程序:
#include <boost/asio.hpp>
namespace a = boost::asio;
using error = boost::system::error_code;
void timer_loop(a::deadline_timer& tim, custom::thread_pool& pool) {
static boost::atomic_int count(0);
tim.expires_from_now(boost::posix_time::milliseconds(10));
tim.async_wait([&](error ec) {
if (!ec && (++count < 100)) {
int id = count;
pool.enqueue([id] {
std::cout << "timer callback " << id << " started on thread " << boost::this_thread::get_id() << "n";
boost::this_thread::sleep_for(boost::chrono::milliseconds(rand()%1000));
std::cout << "timer callback " << id << " completedn";
});
std::cout << "Job " << id << " enqueued" << "n";
timer_loop(tim, pool);
}
});
}
int main()
{
a::io_service svc;
a::deadline_timer tim(svc);
custom::thread_pool pool;
timer_loop(tim, pool);
svc.run();
}
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 为什么在读取文件大小时文件IO速度会发生变化
- Agora.io 虚幻引擎插件构建错误
- 不将数据 socket.io c++(客户端)发送到 nodejs(服务器)socket.io
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- 通过 Tor 服务C++ socket.io 客户端
- 如何使用可视化代码和平台IO将环境变量注入CPP文件?
- 如何读取 google::p rotobuf::io::CodedOutputStream::WriteVarint32
- Conan.io 在编译步骤中或已经在签出时
- C++中真正的异步文件 IO
- 在C++中返回 IO 对象的目的是什么?
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- C++网络 IO、文件处理和网络故障
- Xcode OSX上的C++构建失败,出现多个错误文件IO..不可用:在macOS 10.15中引入
- Android Studio-在现有的旧项目中启用本机C++调试(card.io Android Source)
- 如何检测函数是否执行IO操作
- 如何键入用于const对象的自定义io操纵器
- libevent是否允许在不同的线程中运行timer/io的回调
- IO服务重新启动后,Boost最后期限计时器持续触发