Thread deadlock misery



I have an application with 4 threads GUI, Transmit, Receive, and
StatCalc. The transmit and receive threads both update a counter
variable, and the StatCalc thread periodically updates some statistics
based on those two counters. Unfortunately, the Transmit and StatCalc
threads deadlock and only the receive thread runs.

The GUI thread initiates the Transmit, Receive, and StatCalc threads
virtually simultaneously.

Below is some pseudo-code of what I'm doing

_Transmit_
While(TRUE) {
send a frame;
EnterCriticalSection(&TransmitCounterCrit);
InterlockedIncrement(&TransmitCounter);
LeaveCriticalSection(&TransmitCounterCrit);
Sleep(20);
}

_Receive_
While(TRUE){
receive some frames;
EnterCriticalSection(&ReceiverCounterCrit);
InterlockedExchangeAdd(&ReceiveCounter, framecount);
LeaveCriticalSection(&ReceiveCounterCrit);
wait for next frame;
}

_StatCalc_
While(TRUE) {
//wait for the Transmit and Receive threads to generate data
Sleep(500);
calculate statistics;

//reset the counters
EnterCriticalSection(&TransmitCounterCrit);
InterlockedExchange(&TransmitCounter, 0);
LeaveCriticalSection(&TransmitCounterCrit);
EnterCriticalSection(&ReceiverCounterCrit);
InterlockedExchange(&ReceiveCounter, 0);
LeaveCriticalSection(&ReceiveCounterCrit);
}

If anybody see a way to prevent these threads from dead locking, let
me know.

Thanks,
PaulH

.