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: 68
Joined: Thu Jul 25, 2019 12:31 pm

Multi-threaded application

Post by Ksawery » Sat Oct 12, 2019 12:52 pm

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: 14075
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multi-threaded application

Post by doublemax » Sat Oct 12, 2019 3:18 pm

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: 68
Joined: Thu Jul 25, 2019 12:31 pm

Re: Multi-threaded application

Post by Ksawery » Wed Oct 16, 2019 9:14 am

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
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 241
Joined: Tue Oct 18, 2016 2:31 pm

Re: Multi-threaded application

Post by alys666 » Wed Oct 16, 2019 10:06 am

ubuntu 16.04, wxWidgets 3.0.4

Ksawery
Experienced Solver
Experienced Solver
Posts: 68
Joined: Thu Jul 25, 2019 12:31 pm

Re: Multi-threaded application

Post by Ksawery » Wed Oct 16, 2019 10:59 am

Thanks.

Post Reply