I would like to implement a Boost Asio pattern using a thread for GUI and a worker thread for some socket IO.
The worker thread will use boost::asio::io_service
If you have only one worker, then it's rather easy.
ASIO's handlers are executed by the thread(s) that are calling io_service.run()
. In your case, that means that only one thread, the worker one, can execute callback handler. So you need not to worry about thread safety here.
Your GUI thread, assuming that it has access to one's socket, can call boost::asio::async_write()
without problem. The callback handler, however, will be executed in the worker thread.
From my experience (admitedly limited), I used this pattern:
boost::asio::async_write()
: the worker thread will take care of it.boost::asio::async_read()
, and could be building "business logic packet". What I mean here, is that it construct meaningfull message (could be a subclass of a custom class Packet
or Event
or w/e you what) from raw data.Let me know if its not clear / if I can be of more help.