简体   繁体   English

在1个线程上运行的boost :: asio :: io_service如何唤醒等待状态

[英]How could a boost::asio::io_service running on 1 thread wake up awaiting condition

Thanks for you reply Sam Miller, but i need to implement it on windows. 谢谢您答复Sam Miller,但我需要在Windows上实现它。

Look at this example o wrote: 看这个例子o写道:

boost::mutex mut;
boost::condition_variable cond;

boost::asio::io_service io_service;

boost::asio::deadline_timer t(io_service);

void test_func()
{
    boost::mutex::scoped_lock lk(mut);
    cond.notify_all();
}

void cancel_test_func()
{
    boost::unique_lock< boost::mutex > lk(mut);
    auto canceled_timers = t.cancel();
    cond.wait(lk);
    printf("Canceled...%u.", canceled_timers);
}

int main(int argc, char* argv[])
{
  try
  {
    t.expires_from_now(boost::posix_time::seconds(5));
    t.async_wait(boost::bind(&test_func));

    io_service.post(boost::bind(cancel_test_func));

    boost::thread_group tg;
    tg.create_thread( boost::bind(&boost::asio::io_service::run, &io_service) );
    //tg.create_thread( boost::bind(&boost::asio::io_service::run, &io_service) );

    getchar();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

To make this example works(cancel the timer) i need to uncomment the second thread creation. 为了使该示例工作(取消计时器),我需要取消对第二个线程创建的注释。 With only one thread, i never receive notification. 只有一个线程,我再也不会收到通知。 The question is: is this behavior normal? 问题是:这种行为正常吗?

A condition variable is a synchronous concept, it doesn't integrate well with an event loop like an io_service . 条件变量是一个同步概念,无法与io_service这样的事件循环很好地集成。 If you want asynchronous events on Linux, look at eventfd(2) with with EFD_SEMAPHORE flag for semaphore semantics. 如果要在Linux上进行异步事件,请查看带有EFD_SEMAPHORE标志的eventfd(2) ,以获取信号量语义。 I'm not sure if there's an analogous interface on other platforms 我不确定其他平台上是否有类似的界面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 boost 线程中运行 boost asio io_service - Running boost asio io_service in a boost thread 在一个线程中提升asio运行io_service - boost asio run io_service in a thread boost :: asio io_service线程池 - boost::asio io_service thread pool 如何检查 boost::asio::io_service 是否已开始运行? - How to check if boost::asio::io_service has started running? 当一个线程向 boost::asio::io_service 添加计时器而另一个线程同时运行 io_service::run 时,它是否线程安全? - Is it thread safe when one thread add timer to boost::asio::io_service and the other is running io_service::run at the same time? 从boost :: asio :: io_service :: work作为分离线程运行时捕获异常 - Catching exception from boost::asio::io_service::work running as a detached thread 如何让boost :: asio :: io_service ::在其线程中运行boost :: thread_group更小? - How to make boost::thread_group smaller having boost::asio::io_service::run in its threads? boost::asio::io_service 在线程中,在应用程序退出时不退出 - boost::asio::io_service in thread, does not exit on application exit 对于具有周期性任务的线程池使用boost :: asio io_service? - Using boost::asio io_service for thread pool with periodic tasks? boost :: asio :: io_service ::在多个线程中运行 - boost::asio::io_service::run in more than one thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM