Event / Task Queue Multithreading C++

前端 未结 8 1109
梦如初夏
梦如初夏 2021-02-04 15:18

I would like to create a class whose methods can be called from multiple threads. but instead of executing the method in the thread from which it was called, it should perform t

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 16:10

    You should take a look at the Boost ASIO library. It is designed to dispatch events asynchronously. It can be paired with the Boost Thread library to build the system that you described.

    You would need to instantiate a single boost::asio::io_service object and schedule a series of asynchronous events (boost::asio::io_service::post or boost::asio::io_service::dispatch). Next, you call the run member function from n threads. The io_service object is thread-safe and guarantees that your asynchronous handlers will only be dispatched in a thread from which you called io_service::run.

    The boost::asio::strand object is also useful for simple thread synchronization.

    For what it is worth, I think that the ASIO library is a very elegant solution to this problem.

提交回复
热议问题