Synchronizing worker threads - help w/some logic / approach

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
celstark
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Aug 26, 2005 7:37 pm

Synchronizing worker threads - help w/some logic / approach

Post by celstark »

I'm fairly new to multi-threading, so I'm looking for a little help on how to set something up properly. I've got a function that forks off two worker threads to crunch a bunch of numbers. In the past, I've been able to just have each thread work on half the data and have the main function wait until both are done before continuing. Life was simple.

Life is only a bit more complex now. Now, the two threads need to each work on Part A of a problem before turning to Part B. When both are done with Part A, both may continue to Part B, but neither should start B before both are done with A.

So, how best to have my two threads tell each other the other is done?

Now, I could have a shared counter variable that starts off at 2 and when Part A is done in a thread, it locks, decrements, unlocks and then does some sleeping until it's 0. But, this seems like a programmer not used to threading coming up with a way when there are better ways out there.

Craig
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Hi there,

Most traditional problems with threads can be solved with mailboxes. If you connect two threads with a secure thread safe mailbox you can make some form of inter communication.

Thread 1 works on A, thread 2 works on A too

Thread 1 is done, posts a message for thread 2 that it is done. Thread 1 waits for a mail message from thread 2. Thread 2 posts a message to thread 1, saying it is done. Both threads now can start on B together. You can also make a 3rd referee thread give the order this way by letting that referee post a message that they can both start after it received both messages from the threads, and posts back a message to start.

Honestly, I have solved many problems with a good mailbox class I wonder why there isn't such a standard thing in wxWidgets. My solution was with pthreads so it is not really useful for wxWidgets. But I can imagine simple objects can be passed back and forth to threads this way.

With regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
celstark
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Aug 26, 2005 7:37 pm

Post by celstark »

Thanks Jorgen,

I've got things going right now with that mutex'ed counter. Since I know both threads should take about the same amount of time, it's not too horrific, but just doing some wxMilliSleep calls in a loop until that changes is a kludge it seems.

Craig
Post Reply