views:

77

answers:

1

In asio:: io_service I insert objects. asio:: io_service::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.

+1  A: 

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.

skimobear
I know that this can be implemented using a condition variable,but suggested that this possibility should provide asio::io_service and its members/type_members.Thank you.
niXman