C++ Lock-free Atomic Shared Pointer | SF-Zhou's Blog #161
Replies: 5 comments 2 replies
-
ReferenceCount *Ref() {
++cnt_; // 1
return this;
}
void Deref() {
if (--cnt_ == 0) { // 2
delete this; // 3
}
} 这个不是线程安全的吧, 可能的执行循序有 2 -> 1 -> 3 |
Beta Was this translation helpful? Give feedback.
-
你说的这种情况确实会导致线程不安全,但在 SharedPtr 单线程写或多线程读场景下并不会出现。 |
Beta Was this translation helpful? Give feedback.
-
哦哦,是
获取 Outlook for Android<https://aka.ms/ghei36>
…________________________________
From: SF-Zhou <[email protected]>
Sent: Friday, September 11, 2020 10:21:33 PM
To: SF-Zhou/sf-zhou.github.io <[email protected]>
Cc: lfs <[email protected]>; Mention <[email protected]>
Subject: Re: [SF-Zhou/sf-zhou.github.io] C++ Lock-free Atomic Shared Pointer | SF-Zhou's Blog (#99)
@uestc-lfs<https://github.com/uestc-lfs>
ReferenceCount *Ref() {
++cnt_; // 1
return this;
}
void Deref() {
if (--cnt_ == 0) { // 2
delete this; // 3
}
}
这个不是线程安全的吧, 可能的执行循序有 2 -> 1 -> 3
你说的这种情况确实会导致线程不安全,但在 SharedPtr 单线程写或多线程读场景下并不会出现。
―
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/SF-Zhou/sf-zhou.github.io/issues/99#issuecomment-691123594>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AEO66DWPWTU2I3F2MMRYQPDSFIW63ANCNFSM4N5RGEJA>.
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
ReferenceCount<T> *Acquire() {
uint64_t local = 0;
do {
local = rc_.load();
} while (!rc_.compare_exchange_weak(local, local - (1ull << 48)));
return reinterpret_cast<ReferenceCount<T> *>(local & (-1ull >> 16));
} 这里应该是要加1吧,而不是减1。也就是: } while (!rc_.compare_exchange_weak(local, local + (1ull << 48))); |
Beta Was this translation helpful? Give feedback.
-
https://sf-zhou.github.io/programming/atomic_shared_ptr.html
Beta Was this translation helpful? Give feedback.
All reactions