6488d5363ad1a258126f0c049bd26492f2c49594
[urcu.git] / doc / examples / wfcqueue / cds_wfcq_dequeue.c
1 /*
2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program for any
8 * purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is
10 * granted, provided the above notices are retained, and a notice that
11 * the code was modified is included with the above copyright notice.
12 *
13 * This example shows how to dequeue nodes from a wfcqueue.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <urcu/wfcqueue.h> /* Wait-free concurrent queue */
20 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
21
22 /*
23 * Nodes populated into the queue.
24 */
25 struct mynode {
26 int value; /* Node content */
27 struct cds_wfcq_node node; /* Chaining in queue */
28 };
29
30 int main(int argc, char **argv)
31 {
32 int values[] = { -5, 42, 36, 24, };
33 struct cds_wfcq_head myqueue_head; /* Queue head */
34 struct cds_wfcq_tail myqueue_tail; /* Queue tail */
35 unsigned int i;
36 int ret = 0;
37
38 cds_wfcq_init(&myqueue_head, &myqueue_tail);
39
40 /*
41 * Adding nodes to the linked-list. Safe against concurrent
42 * RCU traversals, require mutual exclusion with list updates.
43 */
44 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
45 struct mynode *node;
46
47 node = malloc(sizeof(*node));
48 if (!node) {
49 ret = -1;
50 goto end;
51 }
52
53 cds_wfcq_node_init(&node->node);
54 node->value = values[i];
55 cds_wfcq_enqueue(&myqueue_head, &myqueue_tail,
56 &node->node);
57 }
58
59 /*
60 * Dequeue each node from the queue. Those will be dequeued from
61 * the oldest (first enqueued) to the newest (last enqueued).
62 */
63 printf("dequeued content:");
64 for (;;) {
65 struct cds_wfcq_node *qnode;
66 struct mynode *node;
67
68 qnode = cds_wfcq_dequeue_blocking(&myqueue_head, &myqueue_tail);
69 if (!qnode) {
70 break; /* Queue is empty. */
71 }
72 /* Getting the container structure from the node */
73 node = caa_container_of(qnode, struct mynode, node);
74 printf(" %d", node->value);
75 free(node);
76 }
77 printf("\n");
78 end:
79 return ret;
80 }
This page took 0.043187 seconds and 3 git commands to generate.