I have a list of threads, and I'm trying to get my main thread to wait for all of the threads in the list to terminate:
while (consumers.Count > 0) consumers[0].Join();
The problem is, this isn't atomic, and I can get an "Index was out of range" exception.
Is there any atomic way to check for the existence of consumers[0] and call consumers[0].Join()?
Note: I can't do
lock (myLocker) { if (consumers.Count > 0) consumers[0].Join(); }
because I don't want to block the other threads from accessing consumers while stuck in Join().