Reading Shared Multi-Queue

<< prev next >>

This example shows how to read a Shared Multi-Queue using the Shared Memory Arena library.

We already ran this program in the last example in Writing Shared Multi-Queue.

Looking at the Source

From examples/tutorial/smq_read.c

#include <unistd.h> /* for usleep() */
#include <signal.h> /* for signal() */
#include <shm_arena.h> /* for Shared Memory Arena Library */
static int running = 1;
static void catcher(int sig)
{
printf("catch signal %d\n", sig);
running = 0;
}
int main(void)
{
smq_t q;
signal(SIGINT, catcher);
q = smq_get(NULL, sizeof(int), 20, "q_count", O_CREAT);
if(!q)
{
printf("smq_get() failed\n");
return 1;
}
while(running)
{
int num;
num = smq_rdlock(q, 0);
while(num > 0)
{
int *ptr;
ptr = smq_read(q);
printf("%d ", *ptr);
fflush(stdout);
num--;
}
sleep(1);
}
return 0;
}

We loop: read-lock the Shared Multi-Queue, read all the entries that are there, unlock the Shared Multi-Queue, and then sleep.

<< prev next >>


Shared Memory Arena version RC-0.0.25