Status & Roadmap
Authors & License
Funding U++
Search on this site

SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

GUI programming and multithreading

Multithreading in GUI programming model has to follow target platform limitations, where most target platforms have a notion of "main thread" and limit GUI actions allowed in non-main threads.


Everything that is manipulating the status of top-level windows (TopWindow or popups) and everything related to event loop must run on the main thread. The only non-const operation allowed to non-main thread on top-level windows is Refresh.


Non-main threads are in general allowed to alter the status of widgets, as long as they serialize access with single global GUI mutex, either with EnterGuiMutex/LeaveGuiMutex or with GuiLock helper guard class (it calls EnterGuiMutex in constructor, LeaveGuiMutex in destructor). GuiLock is reentrant and is optimized for great reentrant performance. It is NOT required to use GuiLock in the even processing methods (like MouseLeft or Key) as U++ locks it in this case itself.


Other means of communication of non-main threads with main thread is PostCallback, which posts an Event to timer queue for immediate processing, but does not wait for it to finish and Ctrl::Call, which passes event to main thread to be processed and returns when the event is executed by the main thread. Both of these methods have quite high latency, GuiLock and direct access should be preferred if possible.


Do you want to contribute?