Multithreading issues with wx application

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
gasko
Earned a small fee
Earned a small fee
Posts: 11
Joined: Mon Apr 13, 2020 11:14 am

Multithreading issues with wx application

Post by gasko »

We have developed an app using wxWidgets. We are now incorporating graphics using Vulkan.

When using threads it is sometimes necessary to "Sleep" the main thread to wait for worker threads to stop. This method works when wxWidgets is not used. However when wxWidgets is used, any sleep function call causes all the threads to sleep.

We can solve this by appropriately calling the wx function "Yield" but this causes "shearing" in the Graphics for some reason.

Is there some other method we can use to cause the main thread to wait for worker threads to finish? This is all done in a highly speed critical process so would prefer an efficient solution which "decouples" wx from the threaded jobs.

Thanks a lot
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Multithreading issues with wx application

Post by doublemax@work »

However when wxWidgets is used, any sleep function call causes all the threads to sleep.
That doesn't make any sense to me.
1) that shouldn't happen at all
2) it shouldn't be related to wxWidets
Maybe you're not describing it well.

Without knowing any details, i'd suggest to just create another thread that waits for the others to finish, and then send an event to the main thread (if needed).
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: Multithreading issues with wx application

Post by jpo234 »

I'm not sure what your worker threads are doing, but the main (GUI) thread should never block. I would get a std::future from std::async, poll them in a single worker thread that then calls wxApp::CallAfter to send the result back to the main thread.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Multithreading issues with wx application

Post by evstevemd »

1. Don't put sleep in the main thread. It will block and freeze the UI
2. If your redrawing functions are in response to thread events then stop sending events fro threads
3. If there are things that need to be disabled, then when you are ready, just disable the parent like panel so that app stops interacting with the user

By any means avoid freezing main thread and sleep is a sure way to do just that!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply