Sign in to follow this  
dangerdaveCS

boost::asio made synchronous

Recommended Posts

dangerdaveCS    122
Hey all. I need to send a message from my main simulation thread to my GUI thread. The problem is I need the simulation thread to block whilst it waits for the GUI thread to finish its job. Is there any way to make boost::asio synchronous? I thought initially that dispatch was synchronous and post was asynchronous, but it seems I'm mistaken (still don't understand - the documentation is severely lacking). Its a shame, because asio has worked wonders elsewhere in my program, where cross-thread fucntion calls are asynchronous, but just for this little bit of code (and perhaps future bits) I need it to block, but can't figure out how. Any help much appreciated,

Share this post


Link to post
Share on other sites
dangerdaveCS    122
Quote:
Original post by Antheus
io_service::dispatch().


But from my experimentation, which may be fatally flawed, dispatch doesn't block. I can't quite fathom what the difference between dispatch and post is...?

I coded a nasty hack workaround using a kind-of 'long polling' technique. I make an infinite loop via boost::asio::deadline_timer.async_wait(), with the wait period pretty long, e.g. 10 secs. In general, the target thread interrupts to say it's finished before those 10 secs are up, so the timer is just cancelled prematurely (if not then the deadline_timer is just restarted). Not an ideal solution, but its something that works.

Share this post


Link to post
Share on other sites
cache_hit    614
Quote:
Original post by dangerdaveCS
Quote:
Original post by Antheus
io_service::dispatch().


But from my experimentation, which may be fatally flawed, dispatch doesn't block. I can't quite fathom what the difference between dispatch and post is...?

I coded a nasty hack workaround using a kind-of 'long polling' technique. I make an infinite loop via boost::asio::deadline_timer.async_wait(), with the wait period pretty long, e.g. 10 secs. In general, the target thread interrupts to say it's finished before those 10 secs are up, so the timer is just cancelled prematurely (if not then the deadline_timer is just restarted). Not an ideal solution, but its something that works.


With dispatch() is possible for your code to begin running in the other thread before the function you invoke dispatch from returns. With post() this is not possible, the calling function must first return. Neither of these is suitable for blocking.


With asio you have to divide synchronous tasks like this into "stages". i.e. make it asynchronous, but specify a callback function that begins the next stage. This has the same effect as making it synchronous.

This is one of the reasons why it's hard to retrofit asio into an existing application, because usually the entire thing is defined as one big synchronous sequence of instructions. Even if you issue the request asynchronously and have it callback to a function which is the next "stage", what is it going to return to? And this is a design problem, because it's not supposed to return to anything, just the asio framework. But like I said it's hard to design code this way unless it was from the beginning.

If this is an issue for you, pass a condition variable to the function you're invoking across threads. At the end of the function, it notifies the condition variable. Meanwhile, immediately after the asynchronous operation is issued, the first thread waits on the condition variable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this