Original Post
Hi Guys,
I have a point in my program that a certain amount of work needs to be done, and main thread execution needs to block until the work is finished.
I have it working, but something about the way I'm doing it is making it lock up sometimes, so there must be a race condition or something.
Here's what I'm doing (pseudo code), can anyone see what's wrong with it, or have any suggestions for how to do it better? It's C++ / windows
Main Thread:
* Random game / engine logic
* Enter a critical section
* Set NumActiveThreads (a volatile unsigned int) to 8 (the number of worker threads i have)
* Leave the critical section
* While NumActiveThreads > 0, Sleep(0) to yield the thread (i do an enter critical section, read value, leave critical section each time)
* back to top
Worker Thread Function:
* While NumActiveThreads == 0, Sleep(0) to yield the thread (i do an enter critical section, read value, leave critical section each time)
* The thread does it's work
* I used an interlocked decriment on NumActiveThreads after the work that this thread is responsible for is done
* While NumActiveThreads != 0, Sleep(0) to yield the thread (i do an enter critical section, read value, leave critical section each time)
* back to top
The idea is that the worker threads sleep until they see NumActiveThreads != 0, then they wake up, do their work and decriment that number. The main thread sets NumActiveThreads to the number of threads, and then waits for that number to hit zero again.
Thanks for any help!
I have a point in my program that a certain amount of work needs to be done, and main thread execution needs to block until the work is finished.
I have it working, but something about the way I'm doing it is making it lock up sometimes, so there must be a race condition or something.
Here's what I'm doing (pseudo code), can anyone see what's wrong with it, or have any suggestions for how to do it better? It's C++ / windows
Main Thread:
* Random game / engine logic
* Enter a critical section
* Set NumActiveThreads (a volatile unsigned int) to 8 (the number of worker threads i have)
* Leave the critical section
* While NumActiveThreads > 0, Sleep(0) to yield the thread (i do an enter critical section, read value, leave critical section each time)
* back to top
Worker Thread Function:
* While NumActiveThreads == 0, Sleep(0) to yield the thread (i do an enter critical section, read value, leave critical section each time)
* The thread does it's work
* I used an interlocked decriment on NumActiveThreads after the work that this thread is responsible for is done
* While NumActiveThreads != 0, Sleep(0) to yield the thread (i do an enter critical section, read value, leave critical section each time)
* back to top
The idea is that the worker threads sleep until they see NumActiveThreads != 0, then they wake up, do their work and decriment that number. The main thread sets NumActiveThreads to the number of threads, and then waits for that number to hit zero again.
Thanks for any help!