When using atomic type in c++ Why does std::atomic<T>::operator= should return a value instead of reference? It’s not like other common assignment operator which returns a reference. From a cppreference site I can get some hint. Unlike most assignment operators, the assignment operators for atomic types do not return a reference to their left-hand arguments. ..
Category : atomic
Given the following simple lock-free linear allocation algorithm I made: bool allocate(size_type size, size_type& offset) { size_type pos; size_type new_pos; do { pos = m_pos.load(std::memory_order_acquire); new_pos = pos + size; if (new_pos > m_end) return false; } while (!m_pos.compare_exchange_weak(pos, new_pos, std::memory_order_acq_rel, std::memory_order_acquire)); offset = pos; return true; } I would like to improve it further ..
I’ve read https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange Atomically compares the object representation (until C++20)value representation (since C++20) of *this with that of expected, and if those are bitwise-equal, replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value stored in *this into expected (performs load operation). So as I understand the code like bool expected=true; extern ..
I am solving leetcode task https://leetcode.com/problems/print-foobar-alternately/ I have two solutions The first — OK (passed tests) I used condition_variable to synchronize threads. // First class FooBar { private: int n; condition_variable cv_; int iteration_; mutex m_; public: FooBar(int n) { this->n = n; iteration_ = 0; } void foo(function<void()> printFoo) { unique_lock<mutex> lock(m_); for (int ..
I’d like, instead of having my threads wait, doing nothing, for other threads to finish using data, to do something else in the meantime (like checking for input, or re-rendering the previous frame in the queue, and then returning to check to see if the other thread is done with its task). I think this ..
The problem that I am trying to solve is the following: I have a big point set which I try to split into smaller ones. I know the sizes of the smaller ones, and I am trying to populate the smaller ones using the bigger one in parallel. In order to do that, I have ..
I have a struct with multi variables packed which is not aligned.Then I make an array of the struct and set up a reader thread and a writer thread to update the variable concurrently.I find error value which only half of the variable is updated from output.I guess this is caused by one variable lay ..
I have two questions: In the general case is it safe to use an atomic as a T and switch between them interchangeably? In the case of a futex is it safe to do the cast? I am aware that performing atomic operations on non-atomic types is undefined behaviour but I cannot find an answer ..
Say that I have the following struct and union: struct Foo { std::atomic<int> a; std::atomic<int> b; }; union Bar { std::atomic<Foo> x; Foo y; } bar; Assuming Foo is always lock free, is it safe to perform (64-bit) atomic loads from bar x while concurrently modifying the 32-bit member variables of bar.y? Source: Windows Questions ..
I suppose that std::atomic sometimes can replace usages of std::mutex. But is it always safe to use atomic instead of mutex? Example code: std::atomic_flag f, ready; // shared // ….. Thread 1 (and others) …. while (true) { // … Do some stuff in the beginning … while (f.test_and_set()); // spin, acquire system lock if ..
Recent Comments