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>
#include <signal.h>
#include <shm_arena.h>
static int running = 1;
static void catcher(int sig)
{
printf("catch signal %d\n", sig);
running = 0;
}
int main(void)
{
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;
while(num > 0)
{
int *ptr;
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.