Re: Using thread synchronization
I'm assuming you have all readers process all the data, otherwise
it's trivial if the data is distributed among the processing threads
and immediately consumed. Have each reader have its own
read index. The oldest of these is the low watermark of data
that still needs to be processed. You also have a write index,
which should help you in determining the oldest in the presense of
frequent wraparounds. The difference from the write index to
the low watermark is your max data span that you need to limit.
The most advanced is the high watermark - this relates to your
minimum span of unprocessed items. So you need to pause the
leading reader when the minimum span falls below the min limit.
When the maximum span shrinks you should signal the writer
to continue. The writer waits for this signal when it exceeds the
maximum span, otherwise it churns out data. Lastly, the writer
needs to wake the leading reader when it posts more data so
the reader can resume processing. Altogether you need two
events, mutex / critical section and n+1 (6 in your case) indices.
All of the indices are protected by a single mutex / critical section
of course. Note you probably want a range instead of a single
value for the maximum span, otherwise you'll have higher
synchronization overhead as the laggard thread continuously
wakes the writer which in turn wakes the leading reader. I'm
not sure why do you care for positive minimum span, however.
I'd simply let the leading reader read up to where the writer has
filled the buffer with data and have it stop there. Then define a
minimum writer lead and only wake the leading reader(s) when
the writer causes the minimum span to exceed this minimum lead.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Mark" <mark@intern.net> wrote in message
news:OTe3LCcEIHA.2268@TK2MSFTNGP02.phx.gbl...
My scenario follows -
one thread fills a circular buffer and
some 5 other threads read the data.
The other details follows-
1. All threads operate independently.
2. Certain minimum and maximum difference limit the reading index of any
thread from the writing index. In other words, since the speed of all
threads are not similar the logic should limit the read index from the
write
index by some minimum difference and also by some maximum one.
3. I uses critical sections to ensure exclusive access to the buffer and
it
works fine.
4. I still have to use some synchronization mechanizm that controls the
indexing difference between read and write threads(as described in 2) but
I can't do it with Critical Section alone.
I'm stack with the question which and how mechanisms of thread
synchronization should I use(events, semaphores etc...)
Can someone advise pleas?
Regards
Mark