Page 1 of 1

Questions regarding threads in wxWidgets

Posted: Wed Oct 26, 2016 1:44 pm
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.

Re: Questions regarding threads in wxWidgets

Posted: Wed Oct 26, 2016 2:40 pm
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.

Re: Questions regarding threads in wxWidgets

Posted: Thu Oct 27, 2016 1:39 pm
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?

Re: Questions regarding threads in wxWidgets

Posted: Thu Oct 27, 2016 2:05 pm
by Laurent Berger