Is it possible, after calling a boost::thread running some instructions, to come back to main thread ?
My code is based around proactor pattern, however a certain function may take some time, so in order not to block the whole program, I’m creating a thread running this function.
When this function is over, I need to call another function, but it has to be run on main thread.
I have a connection pool, that is not thread safe, and I really would like to avoid mutex.
Is there a stable way to run a function on the main thread (call on another thread) ?
Just like in ObjectiveC performSelectorOnMaintThread
I think you might want to look into boost asio strands. They will let you specify which thread (strand) to do some wok on, and it will automatically get queued on that strand.
Note that a strand is actually more like a fiber (meaning: if there are more strands than actual threads, the strands will get multiplexed onto the available threads).
IIRC this won’t however give you chance to explicitely specify the main thread1. However, more often than not this is not the actual requirement: what strands let you do easily is to make sure that operations in a strand run on the same logical thread, i.e. no concurrency is ever possible within a strand.
Trivia: Boost Asio io_service.run() is an example of a message pump
Here is the samples page for Boost Asio: http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/examples.html