The GenerateThreads
function passes the address of local variables to the other thread (temp0
and temp1
). The function then returns immediately after starting the threads. This means that the other threads are now accessing memory that has been freed. It appears that by the time the threads read their param
, the memory changed value to zero, so both threads think that they are the GenerateNumbers
thread.
Adding additional debug print statements would have identified this problem sooner.
Note that you still have a race condition at the end of each block, because you signal the other thread to start before resetting the wake-up event. The other thread may wake up, do its work, then set the wake-up event, all before the first thread returns from SetEvent
. The first thread then resets its event and waits on it, causing it to lose the wake-up event and consequently hang.