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
Synchronizing worker threads - help w/some logic / approach
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
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
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb