1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to splice nodes from a source wfcqueue A into
7 * a destination wfcqueue B.
13 #include <urcu/wfcqueue.h> /* Wait-free concurrent queue */
14 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
17 * Nodes populated into the queue.
20 int value
; /* Node content */
21 struct cds_wfcq_node node
; /* Chaining in queue */
25 int enqueue_values(struct cds_wfcq_head
*head
,
26 struct cds_wfcq_tail
*tail
,
33 for (i
= 0; i
< nr_values
; i
++) {
36 node
= malloc(sizeof(*node
));
41 cds_wfcq_node_init(&node
->node
);
42 node
->value
= values
[i
];
43 cds_wfcq_enqueue(head
, tail
, &node
->node
);
50 void print_queue(struct cds_wfcq_head
*head
,
51 struct cds_wfcq_tail
*tail
,
54 struct cds_wfcq_node
*qnode
;
57 __cds_wfcq_for_each_blocking(head
, tail
, qnode
) {
59 caa_container_of(qnode
, struct mynode
, node
);
60 printf(" %d", node
->value
);
67 int values_A
[] = { -5, 42, 36, 24, };
68 int values_B
[] = { 200, 300, 400, };
69 struct cds_wfcq_head head_A
; /* Queue A head */
70 struct cds_wfcq_tail tail_A
; /* Queue A tail */
71 struct cds_wfcq_head head_B
; /* Queue B head */
72 struct cds_wfcq_tail tail_B
; /* Queue B tail */
75 cds_wfcq_init(&head_A
, &tail_A
);
76 /* Enqueue nodes into A. */
77 ret
= enqueue_values(&head_A
, &tail_A
, values_A
,
78 CAA_ARRAY_SIZE(values_A
));
81 cds_wfcq_init(&head_B
, &tail_B
);
82 /* Enqueue nodes into B. */
83 ret
= enqueue_values(&head_B
, &tail_B
, values_B
,
84 CAA_ARRAY_SIZE(values_B
));
88 print_queue(&head_A
, &tail_A
, "queue A content before splice");
89 print_queue(&head_B
, &tail_B
, "queue B content before splice");
92 * Splice nodes from A into B.
94 printf("Splicing queue A into queue B\n");
95 (void) cds_wfcq_splice_blocking(&head_B
, &tail_B
,
98 print_queue(&head_A
, &tail_A
, "queue A content after splice");
99 print_queue(&head_B
, &tail_B
, "queue B content after splice");
101 cds_wfcq_destroy(&head_A
, &tail_A
);
This page took 0.030706 seconds and 4 git commands to generate.