Questions regarding threads in wxWidgets

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Questions regarding threads in wxWidgets

Post by MagickPanda »

Since I was experiencing threads-related problems when updating grids widgets, I am wondering what is the simplest solution to avoid data corruption when multiple threads modify/read widgets data.

I am reading some C++ 11 book, it says std::mutex/std::recursive mutex is a decent solution to my problem, so is stick to wxWidgets in-build thread functions a better solution since the in-build wrapper/helper functions should be more noob-friendly?

Thanks again.
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: Questions regarding threads in wxWidgets

Post by DenDev »

You should not update UI-elements from threads (please repeat this inside your head 1000 times!). You should post messages from the threads to the main message loop in order to update the grid. In order to prevent multiple thread accessing the same code / memory at once you should use a wxCriticalSection (and a wxCriticalSectionLocker):

http://docs.wxwidgets.org/3.1/classwx_c ... ction.html
http://docs.wxwidgets.org/3.1/classwx_c ... ocker.html

Mutex'es are a heavier instrument than critical sections. Mutex'es should only be used in a environment where multiple processes may access the same resource.
I have a bad habbit of not testing the code I post :D
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: Questions regarding threads in wxWidgets

Post by MagickPanda »

DenDev wrote:You should not update UI-elements from threads (please repeat this inside your head 1000 times!). You should post messages from the threads to the main message loop in order to update the grid. In order to prevent multiple thread accessing the same code / memory at once you should use a wxCriticalSection (and a wxCriticalSectionLocker):

http://docs.wxwidgets.org/3.1/classwx_c ... ction.html
http://docs.wxwidgets.org/3.1/classwx_c ... ocker.html

Mutex'es are a heavier instrument than critical sections. Mutex'es should only be used in a environment where multiple processes may access the same resource.
Thanks for the tips. Is the usage of such multi-thread functions covered in wxwidgets "thread" example/sample source code? or I could find similar content in wiki or on this site?
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Re: Questions regarding threads in wxWidgets

Post by Laurent Berger »

L.B.
Post Reply