I have a “threader” class that is throwing off threads that execute processes. The threads have a callback method in the threader class that get’s called when they finish. My question is how do I know when all the threads the threader has thrown off have finished?
Share
If you keep a count of how many threads you’ve started you can simply increment the count when the thread starts and decrement it when the thread finishes.
Then when the count hits zero you know that all the threads have finished.
You need to take care when dealing with shortlived threads to make sure that you allow the counter to be incremented before it’s decremented. Using some sort of locking or
Interlocked.IncrementandDecrementto modify the counter variable is needed. Source