<< prev | next >> |
This example shows how to write to a shared multi-queue using the Shared Memory Arena library.
A Shared Multi-Queue object (smq) is queue that is built using a shared memory segment(s). So the queue is shared between unrelated processes (or related). The queue is a multi-queue in that all queue objects may read all the data in the queue. It's like all objects in all threads (or processes) can read all the data that was ever written. Shared Multi-Queues use a circular buffer, and so they never fill up, but they do over-write old entries so the queue length, program read and write rates must be made compatible, so know your limiting time rates and don't waste memory.
First lets run this program and see what it does.
In a shell (bash, csh, tcsh) from the directory examples/tutorial/ in two different terminals run:
./smq_writeand
./smq_read
From examples/tutorial/smq_write.c
First we include some header files:
We will use a signal catcher function to terminate the program by un-setting the running
flag.
We get a Shared Multi-Queue object. The Shared Multi-queue's entry size is sizeof(int)
, it will have a queue length of at least 10, and its name is "q_count"
. smq_get() will also get an arena object too.
There is no need to initialize the queue as with a shared memory segment because the reader will see there are no entries in the queue when reading, if there are none.
We then loop write-locking, writing, unlocking, and sleeping until running is zero:
Clearly the usleep() call is just used to keep this example code simple. Such a call should be avoided in good production code.
This is not much different than writing to a shared memory segment except that the data is written in chunks of entries and the entries are queued. That is why we must get the pointer, ptr
, for each write we do, since the returned pointer points to a different queue address for each smq_write() call. The data is not lost by being over-written unless the reader is too slow to read the entries before the queue wraps, overwriting entries that have not been read yet. Any number of readers can start reading the queue entries at any time. All the reader objects will be able to get all the data assuming the queue is large enough for the rates that they are reading and writing. Of course, there may be any number of writers too.
<< prev | next >> |