本文整理汇总了C++中Foo::release_ref方法的典型用法代码示例。如果您正苦于以下问题:C++ Foo::release_ref方法的具体用法?C++ Foo::release_ref怎么用?C++ Foo::release_ref使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Foo
的用法示例。
在下文中一共展示了Foo::release_ref方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Foo
TEST_F(HazptrTest, basic_refcount) {
constructed.store(0);
destroyed.store(0);
Foo* p = nullptr;
int num = 20;
for (int i = 0; i < num; ++i) {
p = new Foo(i, p);
if (i & 1) {
p->acquire_ref_safe();
} else {
p->acquire_ref();
}
}
hazptr_holder hptr;
hptr.reset(p);
for (auto q = p->next_; q; q = q->next_) {
q->retire();
}
int v = num;
for (auto q = p; q; q = q->next_) {
CHECK_GT(v, 0);
--v;
CHECK_EQ(q->val_, v);
}
CHECK(!p->release_ref());
CHECK_EQ(constructed.load(), num);
CHECK_EQ(destroyed.load(), 0);
p->retire();
CHECK_EQ(constructed.load(), num);
CHECK_EQ(destroyed.load(), 0);
hptr.reset();
/* retire enough objects to guarantee reclamation of Foo objects */
for (int i = 0; i < 100; ++i) {
auto a = new Dummy;
a->retire();
}
CHECK_EQ(constructed.load(), num);
CHECK_EQ(destroyed.load(), num);
}
示例2: ready
TEST_F(HazptrTest, mt_refcount) {
constructed.store(0);
destroyed.store(0);
std::atomic<bool> ready(false);
std::atomic<int> setHazptrs(0);
std::atomic<Foo*> head;
int num = 20;
int nthr = 10;
std::vector<std::thread> thr(nthr);
for (int i = 0; i < nthr; ++i) {
thr[i] = std::thread([&] {
while (!ready.load()) {
/* spin */
}
hazptr_holder hptr;
auto p = hptr.get_protected(head);
++setHazptrs;
/* Concurrent with removal */
int v = num;
for (auto q = p; q; q = q->next_) {
CHECK_GT(v, 0);
--v;
CHECK_EQ(q->val_, v);
}
CHECK_EQ(v, 0);
});
}
Foo* p = nullptr;
for (int i = 0; i < num; ++i) {
p = new Foo(i, p);
p->acquire_ref_safe();
}
head.store(p);
ready.store(true);
while (setHazptrs.load() < nthr) {
/* spin */
}
/* this is concurrent with traversal by reader */
head.store(nullptr);
for (auto q = p; q; q = q->next_) {
q->retire();
}
HAZPTR_DEBUG_PRINT("Foo should not be destroyed");
CHECK_EQ(constructed.load(), num);
CHECK_EQ(destroyed.load(), 0);
HAZPTR_DEBUG_PRINT("Foo may be destroyed after releasing the last reference");
if (p->release_ref()) {
delete p;
}
/* retire enough objects to guarantee reclamation of Foo objects */
for (int i = 0; i < 100; ++i) {
auto a = new Dummy;
a->retire();
}
for (int i = 0; i < nthr; ++i) {
thr[i].join();
}
CHECK_EQ(constructed.load(), num);
CHECK_EQ(destroyed.load(), num);
}