stdatomic Questions
2
To write a ref-counted class, I have seen 2 different approaches:
Approach 1:
struct RefCounting1 {
void ref_up() {
m_ref.fetch_add(1, std::memory_order_relaxed);
}
void release() {
if (m_ref...
Headman asked 27/2, 2023 at 20:46
1
Solved
Just when I thought I got some grip around atomics, I see another article. This is an excerpt from GCC wiki, under Overall Summary:
-Thread 1- -Thread 2- -Thread 3-
y.store (20); if (x.load() == ...
Reeducate asked 11/2, 2023 at 18:16
6
Solved
I don't understand, why will be problems without release sequence, if we have 2 threads in the example below. We have only 2 operations on the atomic variable count. count is decremented sequently ...
Editor asked 25/7, 2016 at 10:43
2
Consider an atomic read-modify-write operation such as x.exchange(..., std::memory_order_acq_rel). For purposes of ordering with respect to loads and stores to other objects, is this treated as:
a...
Disentomb asked 4/1, 2021 at 18:50
2
Solved
In C++20, we got the capability to sleep on atomic variables, waiting for their value to change.
We do so by using the std::atomic::wait method.
Unfortunately, while wait has been standardized, wai...
1
Lets suppose following:
I have two processes on Linux / Mac OS.
I have mmap on shared memory (or in a file).
Then in both processes I have following:
struct Data{
volatile int reload = 0; // using...
Dissemblance asked 24/8, 2022 at 11:32
1
Solved
I came across this code for a simple implementation of a barrier (for code that can't use std::experimental::barrier in C++17 or std::barrier in C++20) in C++ Concurrency in Action book.
[Edit]
A b...
Tightrope asked 15/8, 2022 at 4:33
1
Solved
Consider the following test program, compiled and run on an implementation that fully implements C2011 atomics and threads.
#include <stdio.h>
#include <stdatomic.h>
#include <thread...
Fariss asked 2/8, 2022 at 19:6
3
Solved
In C++ we have keyword volatile and atomic class. Difference between them is that volatile does not guarantee thread-safe concurrent reading and writing, but just ensures that compiler will not sto...
Ligan asked 28/10, 2018 at 14:4
3
This is a language-lawyer question.
First of all, does the a.wait() in the following code always get to return?
std::atomic_int a{ 0 };
void f()
{
a.store(1, std::memory_order_relaxed);
a.notify...
Keyhole asked 4/12, 2021 at 18:32
1
Solved
Is there a way that we can write some code to produce a "spurious failures" for the "weak" version of compare_exchange? While the same code should work well for compare_exchange...
Melmon asked 27/6, 2022 at 1:50
1
Solved
If I have atomic_bool flag;, how can I write C code to toggle it that's atomic, portable, and efficient? Regarding "efficient", I'd like it to assemble on x86_64 to lock xorb $1, flag(%ri...
Preraphaelite asked 17/8, 2020 at 16:4
2
Solved
According to cppreference, std::atomic<T>::notify_one() will notify at least one thread that is waiting on said atomic. This means that according to the standard it could unblock more than on...
1
Solved
I've read this Q&A: What is the significance of 'strongly happens before' compared to '(simply) happens before'?
The author gives an outline of an interesting evaluation that wa...
Zamia asked 24/5, 2022 at 22:57
1
Solved
When calling function from an atomic function pointer, like:
#include <atomic>
#include <type_traits>
int func0(){ return 0; }
using func_type = std::add_pointer<int()>::type;
...
Chemise asked 28/4, 2022 at 23:19
1
Solved
On this CPP Con 2017 webinar, Fedor Pikus says: "it has to be direct initialization"
This is the link to the webinar.
What are the differences between these initialization methods? (and ...
2
Solved
When compiling the following piece of code (gcc-4.8, --std=c++11):
#include <atomic>
#include <utility>
#include <cstdint>
struct X {
std::atomic<std::pair<uint32_t, uint...
4
Solved
I am currently reading C++ Concurrency in Action by Anthony Williams. One of his listing shows this code, and he states that the assertion that z != 0 can fire.
#include <atomic>
#include &...
Fishman asked 22/1, 2018 at 14:31
2
Solved
When it comes to implementing CAS Loop using std::atomic, cppreference in this link gives the following example for push:
template<typename T>
class stack
{
std::atomic<node<T>*>...
Majormajordomo asked 31/10, 2019 at 0:8
3
Solved
I'm trying to understand the purpose of std::atomic_thread_fence(std::memory_order_seq_cst); fences, and how they're different from acq_rel fences.
So far my understanding is that the only differen...
Eugeneeugenia asked 4/1, 2022 at 10:46
2
Solved
I have a ring buffer that looks like:
template<class T>
class RingBuffer {
public:
bool Publish();
bool Consume(T& value);
bool IsEmpty(std::size_t head, std::size_t tail);
bool I...
Talca asked 28/12, 2021 at 20:51
2
Solved
Herb Sutter, in his "atomic<> weapons" talk, shows several example uses of atomics, and one of them boils down to following: (video link, timestamped)
A main thread launches severa...
Atrice asked 4/1, 2022 at 16:2
2
Can I perform arithmetic operations on an atomic variable directly?
Since I find the C standard library provides a lot of utility functions like atomic_fetch_add to perform the addition between an ...
Septet asked 3/1, 2022 at 4:6
1
Solved
I'm learning about different memory orders.
I have this code, which works and passes GCC's and Clang's thread sanitizers:
#include <atomic>
#include <iostream>
#include <future>
...
Aloysius asked 31/12, 2021 at 13:59
1
Solved
I read about Herb's atomic<> Weapons talk and had a question about page 42:
He mentioned that (50:00 in the video):
(x86) stores are much stronger than they need to be...
What I don't unde...
Frambesia asked 6/12, 2021 at 17:43
© 2022 - 2024 — McMap. All rights reserved.