The standard computer-sciency producer-consumer book solution
involves a producer thread, a consumer thread, two semaphores
and one mutex. The semaphores can be called “full” and “empty”,
and are initialized full=0 and empty=buffersize: in the
beginning there are zero full slots and “buffersize” empty
slots. The mutex protects the act of reading or writing an
element from/to the common buffer. The producer thread would be
something like
forever do
{
item=getThatItem(); // pick up the item to be produced
empty.wait(); // grab an empty slot, block if no
empty slots
mutex.wait(); // enter the buffer’s critical
section
buffer.insert(item); // update the buffer
mutex.signal(); // release the buffer’s critical
section
full,signal(); // increment the number of
full slots
}
The consumer is similar:
forever do
{
full.wait(); // decrement number of full
slots, block if buffer is empty
mutex.wait(); // enter the buffer’s critical
section
item = buffer.remove(); // remove the item
mutex.signal(); // release the buffer’s critical
section
empty.signal() // increment number of empty slots
}
You can implement the semaphores and the mutex using any
synchronization construct that suits your fancy. Note that
“full” counts full buffer slots, “empty” counts empty slots, and
“mutex” protects the actual buffer access. The empty/full
semaphores work a bit like push-pull, when “empty” goes up,
“full” goes down, and vice-versa: waiting on a full buffer is
provided by the “empty” semaphore, while waiting on an empty
buffer is provided by the “full” semaphore.
Note that I wrote this code in C++ style, in fact you can pick
it up as is if you implement the three classes involved:
semaphore, mutex and buffer. You can easily implement them as
wrappers around the required OS system calls. That will allow
you to easily experiment with different implementations and zero
in on the one you like the best.
Hope this helps!
Alberto.
----- Original Message -----
From: “Martin O’Brien”
To: “Windows System Software Devs Interest List”
Sent: Thursday, September 15, 2005 9:56 AM
Subject: Re: [ntdev] Producer-Consumer problem
You definitely do not want to poll, although, unless I’m missing
something, I’m not sure how that would even be possible.
>>> xxxxx@networkgeneral.com 09/15/05 4:38 AM >>>
Hello,
lets assume there is a memory-block shared between user-mode &
kernel-mode. The produce & consumers of the shared memory can be
user-mode or kernel-mode threads. For simple, consider one
producer and
multiple consumers. And producer-thread writes data sometimes in
0 rate
and sometimes in very high rate.
My question is, how should the consumer read the data?
1) Waiting on a events until the writer has set the event after
writing
a new data?
2) Or Polling continuously for a new data?
Which one is better?
Thanks,
Raja
—
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: unknown lmsubst tag
argument: ‘’
To unsubscribe send a blank email to
xxxxx@lists.osr.com
—
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: unknown lmsubst tag
argument: ‘’
To unsubscribe send a blank email to
xxxxx@lists.osr.com