C++ 截止时间计时器的非阻塞升压io_服务
在阅读boost::asio::deadline\u timer的文档后,似乎io\u service::run()和处理程序方法是在同一线程上调用的。在后台线程上运行io_服务对象时,有没有方法在一个线程上创建计时器?为了好玩和荣耀,下面介绍如何将线程队列与asio截止时间计时器相结合,以从截止时间计时器中分派非阻塞任务:C++ 截止时间计时器的非阻塞升压io_服务,c++,multithreading,boost-asio,C++,Multithreading,Boost Asio,在阅读boost::asio::deadline\u timer的文档后,似乎io\u service::run()和处理程序方法是在同一线程上调用的。在后台线程上运行io_服务对象时,有没有方法在一个线程上创建计时器?为了好玩和荣耀,下面介绍如何将线程队列与asio截止时间计时器相结合,以从截止时间计时器中分派非阻塞任务: #ifndef HEADER_GUARD_CUSTOM_THREADPOOL_HPP #define HEADER_GUARD_CUSTOM_THREADPOOL_HPP
#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
#ifndef头(保护)自定义(线程池)HPP
#定义标头\u保护\u自定义\u线程池\u HPP
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
命名空间自定义{
使用名称空间boost;
类线程池
{
私人:
互斥mx;
条件变量cv;
类型定义功能作业;
std::deque_队列;
线程组池;
原子弹爆炸关机;
静态无效工作线程(线程池和q)
{
while(可选作业=q.dequeue())
(*工作)();
}
公众:
线程池():关闭(false){
//LOG_INFO_MESSAGE您想实现什么?您可以使用您的服务运行任意多个IO线程。因此,如果您想每隔一段时间向后台线程发送一次信号,只需提出一个条件变量或其他什么?感谢sehe的回答。实际上,需要将进行回调的线程设置为非阻塞线程。在正常情况下,在当我们调用io_service::run()方法时,它将被阻塞,直到计时器过期。因此两个线程都需要独立。
#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 << " completed\n";
});
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();
}