MCPcopy Create free account
hub / github.com/cameron314/concurrentqueue / try_dequeue

Method try_dequeue

concurrentqueue.h:1147–1178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1145 // Never allocates. Thread-safe.
1146 template<typename U>
1147 bool try_dequeue(U& item)
1148 {
1149 // Instead of simply trying each producer in turn (which could cause needless contention on the first
1150 // producer), we score them heuristically.
1151 size_t nonEmptyCount = 0;
1152 ProducerBase* best = nullptr;
1153 size_t bestSize = 0;
1154 for (auto ptr = producerListTail.load(std::memory_order_acquire); nonEmptyCount < 3 && ptr != nullptr; ptr = ptr->next_prod()) {
1155 auto size = ptr->size_approx();
1156 if (size > 0) {
1157 if (size > bestSize) {
1158 bestSize = size;
1159 best = ptr;
1160 }
1161 ++nonEmptyCount;
1162 }
1163 }
1164
1165 // If there was at least one non-empty queue but it appears empty at the time
1166 // we try to dequeue from it, we need to make sure every queue's been tried
1167 if (nonEmptyCount > 0) {
1168 if ((details::likely)(best->dequeue(item))) {
1169 return true;
1170 }
1171 for (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {
1172 if (ptr != best && ptr->dequeue(item)) {
1173 return true;
1174 }
1175 }
1176 }
1177 return false;
1178 }
1179
1180 // Attempts to dequeue from the queue.
1181 // Returns false if all producer streams appeared empty at the time they

Callers 2

Calls 5

next_prodMethod · 0.80
loadMethod · 0.45
size_approxMethod · 0.45
dequeueMethod · 0.45
fetch_addMethod · 0.45

Tested by

no test coverage detected