I’m using CreateThread() for my 4 threads.
I would like to run all my threads simultaneously, but wait for all 4 to finish before continuing with the main thread.
I used an int and increased it at the start of the thread, then decreased it at the end, then in my main thread I used a while loop to hold while the number is over a certain value… however this didn’t seem to work correctly.
Is this possible?
You can use the mechanism of signaled states and the WaitForMultipleObjects function to wait for the events or threads themselves (pointed to by their handles) to reach a signalled state.
By simply sharing a single variable among those threads you’re probably running into synchronization problems, especially when they are spread among your CPU’s cores.
If you want to modify a shared value atomically without using synchronization mechanisms, use the “Interlocked*” functions like InterlockedIncrement, although that doesn’t completely guarantee that there will be no problems. Don’t use that method as a synchronization mechanism anyway.