[asio] posting a function to an io_service object
Hi,
I am trying to call a function by posting it to an io_service object
running in a different thread from the main thread of the application.
The problem is the function always gets called from the main thread.
Basically I would like to do this with asio:
#include <iostream>
#include
Using Boost.Asio, f() always gets called from the main thread:
You use io_service incorrectly, please see asio tutorial/examples.
while (ios1.poll() > 0) ; while (ios2.poll() > 0) ;
io_services are already running - in other threads - why do you poll() them here?
You use io_service incorrectly, please see asio tutorial/examples.
while (ios1.poll() > 0) ; while (ios2.poll() > 0) ;
io_services are already running - in other threads - why do you poll() them here? In this example, poll() is there to execute the f() handlers and wait for them to finish.
You are right, not polling the io_services from the main thread makes the f() calls run in the other threads as intended. In that case is there a way to check from the main thread if the io_services finished executing the posted handlers (some kind of a peek())? I have a program where there are several parallel io_threads and I want to run two distinct library functions in each io_service first thing an io_service is started and last thing before an io_service is stopped. These functions are posted from the main thread. My problem is with the function that is to be executed last: how can I wait for that to be executed before stopping the io_service and joining the thread? Calling poll()/dispatch() from the main thread is not an option, it seems. Vil
My problem is with the function that is to be executed last: how can I wait for that to be executed before stopping the io_service and joining the thread?
Usually you don't have to stop io_service explicitly - when it runs
out of work, it stops by its own. So if all you need is to perform
several functions/functors on io_service thread - just post them to
io_service, and join io_service thread(s):
// pseudo-code!
{
asio::io_service io;
io_.post(functor1);
io_.post(functor2);
thread t(&io_service::run, &io);
t.join(); // when io_service runs out of jobs, run() exits.
}
If you need to get more control over io_service::run "lifetime", you
can use work object, like in your example:
// pseudo-code!
{
asio::io_service io;
shared_ptr
Thank you for your answer, Igor. I need a work object as the io_service not always has real work to do, resetting the work shared_ptr before join() is the right solution for me. A conditional variable is definitely a more elegant approach, I might give it a go. :) Happy new year to you all. Vil
-----Original Message-----
You are right, not polling the io_services from the main thread makes the f() calls run in the other threads as intended. In that case is there a way to check from the main thread if the io_services finished executing the posted handlers (some kind of a peek())?
The run command will return if you get rid of the two work objects. Then, you could just wait for the threads to join.
I have a program where there are several parallel io_threads and I want to run two distinct library functions in each io_service first thing an io_service is started and last thing before an io_service is stopped. These functions are posted from the main thread. My problem is with the function that is to be executed last: how can I wait for that to be executed before stopping the io_service and joining the thread? Calling poll()/dispatch() from the main thread is not an option, it seems.
Look into writing a custom service object. I think that the logging example is a good place to start. There is an entry point that gets called when the io_service is being stopped. ~Dan
participants (3)
-
Casimiro, Daniel C CIV NUWC NWPT
-
Igor R
-
Vilhelm Sundberg