本文整理汇总了C++中Foo::retire方法的典型用法代码示例。如果您正苦于以下问题:C++ Foo::retire方法的具体用法?C++ Foo::retire怎么用?C++ Foo::retire使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Foo
的用法示例。
在下文中一共展示了Foo::retire方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: h
TEST_F(HazptrTest, Array) {
struct Foo : hazptr_obj_base<Foo> {
int a;
};
for (int i = 0; i < 100; ++i) {
Foo* x = new Foo;
x->a = i;
hazptr_array<10> hptr;
// Protect object
hptr[9].reset(x);
// Empty array
hazptr_array<10> h(nullptr);
// Move assignment
h = std::move(hptr);
// Retire object
x->retire();
// Unprotect object - hptr2 is nonempty
h[9].reset();
}
{
// Abnormal case
hazptr_array<HAZPTR_TC_SIZE + 1> h;
hazptr_array<HAZPTR_TC_SIZE + 1> h2(std::move(h));
}
}
示例2:
TEST_F(HazptrTest, Move) {
struct Foo : hazptr_obj_base<Foo> {
int a;
};
for (int i = 0; i < 100; ++i) {
Foo* x = new Foo;
x->a = i;
hazptr_holder hptr0;
// Protect object
hptr0.reset(x);
// Retire object
x->retire();
// Move constructor - still protected
hazptr_holder hptr1(std::move(hptr0));
// Self move is no-op - still protected
hazptr_holder* phptr1 = &hptr1;
CHECK_EQ(phptr1, &hptr1);
hptr1 = std::move(*phptr1);
// Empty constructor
hazptr_holder hptr2(nullptr);
// Move assignment - still protected
hptr2 = std::move(hptr1);
// Access object
CHECK_EQ(x->a, i);
// Unprotect object - hptr2 is nonempty
hptr2.reset();
}
}
示例3: 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);
}