| 138 | // the `std::for_each()`. |
| 139 | template <typename F> |
| 140 | void for_each(F&& f) |
| 141 | { |
| 142 | auto end = head.load(); |
| 143 | auto node = tail; |
| 144 | |
| 145 | for (;;) { |
| 146 | node = node->next.load(); |
| 147 | |
| 148 | // We are following the linked structure until we reach the end |
| 149 | // node. There is a race with new nodes being added, so we limit |
| 150 | // the traversal to the last node at the time we started. |
| 151 | if (node == nullptr) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | f(node->element); |
| 156 | |
| 157 | if (node == end) { |
| 158 | return; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Single consumer only. |
| 164 | bool empty() |
no outgoing calls