开发者

boost.asio. The possibility to expect the completion of any object in queue is necessary

In asio:: io_service I insert objects. asio:: io_service:开发者_C百科:run() runs in several threads. The possibility to expect the completion of any object in queue is necessary. For example:

template <typename T>
struct handler {
   void operator()() {
      ....
   }
   T get() const {...}
};

asio::io_service ios;
ios.post(handler());
  1. How I can refer to the object in queue?
  2. How can I pause the main program loop, untill handler::operator() is executed?

Thanks.


Here's what I know so far:
1. Several handlers are executing on several threads.
2. Handlers run independently of each other. There is no synchronization needed between threads for data/race conditions.
3. Get can be called on anyone handler. When called the handler should stop calculating and let another thread call handler::get().

This really seems more like a multi-threading / concurrency question then a boost::asio question. At the moment I do not see a need to use an io_service. It seems like several threads could just be started without an io_service and some synchronization could be used between the threads.

The thread calling Handler::get() needs to wait until the io_service thread running operator() completes its calculation before it can return.

Consider using a condition variable. The handler::get() method can wait until the condition is met (i.e. operator() finishes its calculation). The io_service thread that runs operator() would notify the main thread through the condition variable.

An example of one thread notifying another thread via a condition variable is here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜