AtomicSharedPtr | Free implementation of std : : atomic std : : shared_ptr
kandi X-RAY | AtomicSharedPtr Summary
kandi X-RAY | AtomicSharedPtr Summary
Lock-Free implementation of std::atomic & several Lock-Free data structures based on it.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of AtomicSharedPtr
AtomicSharedPtr Key Features
AtomicSharedPtr Examples and Code Snippets
Community Discussions
Trending Discussions on AtomicSharedPtr
QUESTION
I'm trying to implement a lock-free wrapper via std::atomic>
to operate over non-trivial objects like containers.
I found some relevant pieces of information in these two topics:
But it still isn't what I need.
Give an example:
...ANSWER
Answered 2021-Dec-21 at 20:57First, a sidenote. std::atomic>
gives atomic access to the pointer, and provides no synchronization whatsoever for the T
. That's super important to note here. And your code shows that you're trying to synchronize the T
, not the pointer, so the atomic
is not doing what you think it is. In order to use std::atomic>
, you must treat the pointed-at T
as const
.
There's two ways to handle read-modify-write with arbitrary data in a thread safe way. The first is, obviously, to use locks. This is usually faster to execute and due to its simplicity, usually less buggy, and is therefore highly suggested. If you really want to do this with atomic operations, it's difficult, and executes slower.
It usually looks something like this, where you make a deep copy of the pointed-at data, mutate the copy, and then attempt to replace the old data with the new data. If someone else has changed the data in the meantime, you throw it all away and start the whole mutation over.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install AtomicSharedPtr
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page