2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
13 * This example shows how to enqueue nodes into a RCU lock-free queue.
14 * This queue requires using a RCU scheme.
20 #include <urcu/urcu-memb.h> /* RCU flavor */
21 #include <urcu/rculfqueue.h> /* RCU Lock-free queue */
22 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
25 * Nodes populated into the queue.
28 int value
; /* Node content */
29 struct cds_lfq_node_rcu node
; /* Chaining in queue */
32 int main(int argc
, char **argv
)
34 int values
[] = { -5, 42, 36, 24, };
35 struct cds_lfq_queue_rcu myqueue
; /* Queue */
40 * Each thread need using RCU read-side need to be explicitly
43 urcu_memb_register_thread();
45 cds_lfq_init_rcu(&myqueue
, urcu_memb_call_rcu
);
50 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
53 node
= malloc(sizeof(*node
));
59 cds_lfq_node_init_rcu(&node
->node
);
60 node
->value
= values
[i
];
62 * Both enqueue and dequeue need to be called within RCU
63 * read-side critical section.
65 urcu_memb_read_lock();
66 cds_lfq_enqueue_rcu(&myqueue
, &node
->node
);
67 urcu_memb_read_unlock();
71 urcu_memb_unregister_thread();