I have written a C++ 11 code using asio to send/receive the packets from network. To work with asio, asio::io_service::run()
need to be called and the main thread will be waiting in this function.
Now, in one of the applications, I need to develop a Gui for which I need to use Qt 4.8. But for a Qt Gui application, the main thread need to wait in QApplication::exec()
. Though Qt has its own libraries to send/receive data from Network, I would like to use the code with asio.
How can I use asio along with Qt?
Asked
Active
Viewed 199 times
0

Soo
- 885
- 7
- 26
-
`boost::asio`? Then maybe this is related: https://stackoverflow.com/questions/39481072/boost-asio-with-qt – 463035818_is_not_an_ai Mar 30 '20 at 19:18
-
Call `io_service::run` on a different thread. – Miles Budnek Mar 30 '20 at 19:30
1 Answers
1
Without seeing your code.. a high-level generic approach:
- Call
io_context::run()
in a new thread. All async handlers will execute in this thread. - Use a
mutex
to ensure the data model can be safely shared between ASIO and the GUI. Lock the mutex consistently any time the data is read or updated. - Use Qt signals and slots and/or
condition_variable
to signal the GUI from ASIO completion handlers whenever the UI needs to be updated. Do not touch the GUI directly from ASIO code, as only the UI thread may update the UI.

rustyx
- 80,671
- 25
- 200
- 267