Multi-threaded application Topic is solved

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
Ksawery
Experienced Solver
Experienced Solver
Posts: 83
Joined: Thu Jul 25, 2019 12:31 pm

Multi-threaded application

Post by Ksawery »

Hello,

I'm currently implementing an application where I would like to make use of multiple threads (probably 3 threads, but maybe more in the future - it would be nice if I could create any number of threads based on a single parameter). Each thread will handle communication via Modbus RTU or TCP/IP (the application will be a communication gateway essentially), and each thread will need to have shared access to the same data structure. I've created a separate class to handle all Modbus communication in the background. Other wxWidgets frames will periodically access the public structure in this class to display the latest data.

Which wxWidgets classes/methods would you recommend that I use. Ideally, I'd like the threads to work as fast as possible and be able to implement some kind of data protection on the shared structure. I also don't need the threads to trigger any events in the other wxWidgets frames - just need them to work in the background and update the structure, while avoiding clashes in data access. Would wxThreadHelper do the job, or are there simpler/better ways of implementing this?

Many thanks,
Ksawery
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multi-threaded application

Post by doublemax »

Just use wxThread for the threads and wxMutex/wxCriticalSection to protect access to shared resources.
Use the source, Luke!
Ksawery
Experienced Solver
Experienced Solver
Posts: 83
Joined: Thu Jul 25, 2019 12:31 pm

Re: Multi-threaded application

Post by Ksawery »

Thank you, I'm currently following your suggestion, but haven't tested the application yet. Quick question - how does wxCiticalSection work exactly? As I understand, all threads trying to access the same memory location should use the same wxCriticalSection?

Regards,
Ksawery
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: Multi-threaded application

Post by alys666 »

ubuntu 20.04, wxWidgets 3.2.1
Ksawery
Experienced Solver
Experienced Solver
Posts: 83
Joined: Thu Jul 25, 2019 12:31 pm

Re: Multi-threaded application

Post by Ksawery »

Thanks.
Post Reply