2

I'm writing a C++ application whose main window needs to receive real-time data from a server and draw plots and histograms in realtime based on this data. I'm using GTK3 (actually its C++ binding gtkmm) and Cairo.

In particular, data is received every 1 second from the network, and refresh happens every time the data is received, thus every 1 second. Refresh is done by calling the invalidate_rect() method for the entire drawing area, whose on_draw() even redraws all figures and plots using the newly received data.

Now, the application works but it's extremely unreliable. In particular, it freezes very often, especially when the CPU load increases. The CPU usage of my application, as well as memory, are very low. Suddenly the window becomes grey and unresponsive, and I need to kill it with Ctrl-C, since even pressing the window close icon doesn't work.

I'm wondering: is it the wrong approach to call invalidate_rect() in the scenario above? What is a better way, using GTKMM/Cairo, to obtain smooth graphics in a reliable way?

  • What is your thread structure? Can you somehow integrate the server polls into the GTK+ main loop using GIO? Also why `invalidate_rect()` and not `queue_draw_area()`? And redrawing more than you need can lead to speed issues; since you were already using `invalidate_rect()` you should have only passed the part you needed to update. – andlabs Apr 10 '16 at 01:05
  • The main thread (which blocks on `app->run(win)`) spawns another one that receives the data from the network (once per second) and calls the `invalidate_rect()` for the drawing area every time. The entire drawing area need to be redrawn as it is packed with dynamically changing figures. Tried `queue_draw_area()` instead, but didn't solve the problem. –  Apr 10 '16 at 01:14
  • You can't call `queue_draw_area()` from the other thread. You need to queue it to run on the GTK+ thread. In plain C this would be done with `gdk_threads_add_idle()`, but I don't know what it would be with gtkmm. – andlabs Apr 10 '16 at 01:52
  • Thanks @andlabs that solved the problem. By calling the `invalidate_rect()` from the main thread (via a periodic timer event) the application is now very stable. –  Apr 11 '16 at 16:07

0 Answers0