Need Thread Advice

Tech-Archive recommends: Fix windows errors by optimizing your registry



I have an application that utilizes 2 threads for communications; 1 for
receive and 1 for transmit. The threads are derived classes of CWinThread.
The code is written so that when a button is clicked, I open a com port and
immediately send data out the port. There was a timing problem in that when
the port was opened, the threads were not running at the time that the
transmit operation was begun. To overcome this, I added a Ready flag in the
threads and when I open the port, I check this flag to make sure the thread
is running before exiting the open com function.

/* pseudo code */
OpenCom()
{
/*... open comm stuff */
pthread = CreateThread(......);

pthread->ResumeThread();

while ( !pthread->Ready ) ;
}

The debug code worked ok, but the release code hung in the while() loop. I
realized I had to free up time for the thread so I changed it to
while ( !pthread->Ready ) Sleep(5);

This seems to work. My question is, is there a better way to accomplish
this.

Thanks


.


Quantcast