Thread deadlock misery
- From: "PaulH" <paul.heil@xxxxxxxxx>
- Date: 12 Mar 2007 15:12:30 -0700
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
.
- Follow-Ups:
- Re: Thread deadlock misery
- From: Bruno van Dooren [MVP VC++]
- Re: Thread deadlock misery
- From: Doug Harrison [MVP]
- Re: Thread deadlock misery
- Prev by Date: Re: UN-using a namespace?
- Next by Date: Re: ReadFille error caused by USB sniffer
- Previous by thread: ReadFille error caused by USB sniffer
- Next by thread: Re: Thread deadlock misery
- Index(es):