0

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?

Soo
  • 885
  • 7
  • 26

1 Answers1

1

Without seeing your code.. a high-level generic approach:

  1. Call io_context::run() in a new thread. All async handlers will execute in this thread.
  2. 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.
  3. 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