Re: Thread synchronization
"Mark" <mark@intern.net> wrote in message
news:uFRLv6aEIHA.1184@TK2MSFTNGP04.phx.gbl...
Thanks Mr. McPhillips,
I already use critical section to enable exclusive access to the buffer,
but how can I pause a reading thread that exceeds its read values
limitation with critical section?
I thought that I must use in addition to critical sections, at list events
and WaitForSingleEvnet function(there is some close example in the msdn
here http://msdn2.microsoft.com/en-us/library/ms686915.aspx that uses only
events without critical sections).
Can you farther help me?
If I understand correctly, you want a reading thread to pause when the
writing index and reading index of a circular buffer are at certain
difference limits. I think you can do that with logic that uses a critical
section and a manual reset event. Inside the critical section the writing
thread can write to the queue, update the write index, and test the
difference between indexes to decide if reading is OK. It would SetEvent to
permit reading, or ResetEvent when no reading should occur. Then it exits
from the critical section.
The reading thread would call WaitForSingleObject on the event. When that
returns enter the critical section, test the indexes to see if it is still
OK to read, and if so read the queue and update the reading index, then exit
the critical section. If the reading thread finds that it is not OK to read
it should reset the event and return from the critical section. That will
stop the reading thread from further reading until the writing thread sets
the event again.
Good Luck.
--
Scott McPhillips [VC++ MVP]