Multithreaded GTK drawing support

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
StSt1
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Oct 23, 2020 6:49 am

Multithreaded GTK drawing support

Post by StSt1 » Sat Nov 21, 2020 2:33 pm

thread.cpp
GTK Multithreaded drawing sample file
(17.21 KiB) Downloaded 13 times
thread.cpp
GTK Multithreaded drawing sample file
(17.21 KiB) Downloaded 13 times
Multithreaded drawing on the GTK platform has shown to be a source of trouble. Unfortunately, the sample code of thread.cpp gives not enough information on how real indpendendent drawing in multiple instances of wxDialogs can be achieved.

When trying to run multiple drawing threads at once, the X11 subsystem stops the application and raises an error message that a call to XInitThreads() is missing. Introducing this in the ::OnInit method of the wxApp startup procedure the thread starts, but produces frequently runtime errors. The conclusion to this was, that GTK faces racing conditions leading to abnormal program termination.

Luckily there is a detailled article on the Cairo portal discussing multithreaded drawing with GTK in more detail https://www.cairographics.org/threaded_ ... with_cairo. The solution suggested here was to let GTK know the application utilizes multiple threads (raise thread awareness) and to protect critical operations.

Based on the explanations and techniques given in this discussion I was able to rework the thread.cpp example in way to get a stable version. For those who concern find the sample attached to this post. The implementation was tested based on wxWidgets-3.0.3 on RHEL6 system.

Regards,

Stefan
Attachments
SampleView.png
Multiple open draw dialogs

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2660
Joined: Sun Jan 03, 2010 5:45 pm

Re: Multithreaded GTK drawing support

Post by PB » Sat Nov 21, 2020 5:57 pm

I do not use GTK, so I did not check the files but...

The wxWidgets official position is: Do not access any GUI classes from the secondary thread, so it is no wonder its thread sample does not do that.

Additionally, with drawing the issue is that wxGDIObject-derived classes are reference-counted and not thread-safe. There was a thread on wxUsers or wxDev discussion group some years ago (IIRC GTK-specific) and I think the conclusion was basically that it generally is not safe, no matter what you do. I think it was this thread: https://groups.google.com/g/wx-users/c/ ... jVh1VPBAAJ

But I am not saying it is impossible: I would recommend asking in wxUsers where the core developers respond (this is a user forum).

Post Reply