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 pop all nodes from a lfstack.
19 #include <urcu/lfstack.h> /* Lock-free stack */
20 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
23 * Nodes populated into the stack.
26 int value
; /* Node content */
27 struct cds_lfs_node node
; /* Chaining in stack */
30 int main(int argc
, char **argv
)
32 int values
[] = { -5, 42, 36, 24, };
33 struct cds_lfs_stack mystack
; /* Stack */
36 struct cds_lfs_node
*snode
, *sn
;
37 struct cds_lfs_head
*shead
;
39 cds_lfs_init(&mystack
);
44 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
47 node
= malloc(sizeof(*node
));
53 cds_lfs_node_init(&node
->node
);
54 node
->value
= values
[i
];
55 cds_lfs_push(&mystack
, &node
->node
);
59 * Pop all nodes from mystack into shead. The head can the be
62 shead
= cds_lfs_pop_all_blocking(&mystack
);
65 * Show the stack content, iterate in reverse order of push,
66 * from newest to oldest. Use cds_lfs_for_each_safe() so we can
67 * free the nodes as we iterate.
69 printf("mystack content:");
70 cds_lfs_for_each_safe(shead
, snode
, sn
) {
72 caa_container_of(snode
, struct mynode
, node
);
73 printf(" %d", node
->value
);
78 cds_lfs_destroy(&mystack
);
This page took 0.036566 seconds and 5 git commands to generate.