本文整理汇总了C++中node::Ptr::ptr方法的典型用法代码示例。如果您正苦于以下问题:C++ Ptr::ptr方法的具体用法?C++ Ptr::ptr怎么用?C++ Ptr::ptr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类node::Ptr
的用法示例。
在下文中一共展示了Ptr::ptr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: slowpathLock
void slowpathLock(Node::Ptr oldTail) {
Node me;
Node::Ptr curTail;
bool newThreads;
// Step one, put ourselves at the back of the queue
for (;;) {
Node::Ptr newTail = Node::Ptr(&me, oldTail.tag());
// Enqueue ourselves...
if (tail_.compare_exchange_strong(oldTail, newTail,
std::memory_order_acq_rel,
std::memory_order_relaxed)) break;
// OK, maybe the whole thing is just unlocked now?
if (oldTail == Node::Ptr(nullptr, 0)) {
// If so, try the top level lock
if (tail_.compare_exchange_strong(oldTail,
Node::Ptr(nullptr, 1),
std::memory_order_acquire,
std::memory_order_relaxed))
goto out;
}
}
// Step two: OK, there is an actual queue, so link up with the old
// tail and wait until we are at the head of the queue
if (oldTail.ptr()) {
// * Writing into the oldTail is safe because threads can't
// leave unless there is no thread after them or they have
// marked the next ready
oldTail->next.store(&me, std::memory_order_release);
while (!me.ready.load(std::memory_order_acquire)) delay();
}
// Step three: wait until the lock is freed
while ((curTail = tail_.load(std::memory_order_relaxed)).tag()) {
delay();
}
// Step four: take the lock
for (;;) {
assert_eq(curTail.tag(), 0);
assert_ne(curTail.ptr(), nullptr);
newThreads = curTail.ptr() != &me;
// If there aren't any waiters after us, the queue is
// empty. Otherwise, keep the old tail.
Node *newTailP = newThreads ? curTail : nullptr;
Node::Ptr newTail = Node::Ptr(newTailP, 1);
if (tail_.compare_exchange_strong(curTail, newTail,
std::memory_order_acquire,
std::memory_order_relaxed)) break;
}
// Step five: now that we have the lock, if any threads came
// in after us, indicate to the next one that it is at the
// head of the queue
if (newThreads) {
// Next thread might not have written itself in, yet,
// so we have to wait.
Node *next;
while (!(next = me.next.load(std::memory_order_acquire))) delay();
next->ready.store(true, std::memory_order_release);
}
out:
return;
}