Can I specify the number of threads running for WxThread?

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
dingjun.chen
Earned a small fee
Earned a small fee
Posts: 16
Joined: Wed Aug 23, 2023 9:05 pm

Can I specify the number of threads running for WxThread?

Post by dingjun.chen »

Hi, guys,

I want to run WxThreads in ONE thread because I want to write some data into a file for tracking. In case of multi-threads, the output file is messed up. I guess there are multiple threads writing into the output file.

Can I run wxThread with only ONE thread? How to do it if possible? Is there any way to dump data in case of only one thread running?

I look forward to your help and your reply is highly appreciated.

Thanks and Regards,

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

Re: Can I specify the number of threads running for WxThread?

Post by PB »

It seems you need to learn how multi-threading works, each thread is separate, you cannot run "run WxThreads in ONE thread".

There is only as many threads as you create, wxWidgets does not create any on its own (or at least not visible to the user).

You seem to be looking either for a thread pool (which wxWidgets does not provide) or an access synchronization object, such as semaphore, mutex, or critical section, which wxWidgets has.

Regardless, it is not recommended to use wxThread and company in new code, they are relics from times C++ did not have multi-threading support.
User avatar
shawnhcorey
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Nov 19, 2012 3:29 pm
Location: The Great White North

Re: Can I specify the number of threads running for WxThread?

Post by shawnhcorey »

Usually, all the I/O is done in the main (aka root) thread to avoid these problems. Threads are used only for processing. They return their results to the main thread for output.
WARNING: Highly caffeinated ☕. Approach with caution 🚧.
Post Reply