atomic Questions

2

Consider this example: #include <iostream> #include <atomic> #include <thread> #include <chrono> #include <cassert> int main(){ std::atomic<int> v = 0; std::at...
Eijkman asked 23/10 at 13:20

1

Solved

Consider this example: #include <iostream> #include <atomic> #include <thread> struct SpinLock{ std::atomic<bool> state; void lock(){ bool expected = false; while(!stat...
Tricksy asked 17/10 at 13:41

1

Solved

#include <memory> #include <atomic> #include <iostream> #include <thread> #include <cassert> int main() { std::atomic<bool> flag = {false}; std::atomic<int&...

2

Solved

Consider this example: std::atomic<int> v = {0}; // thread 1: for(int i = 0; i<999999;i++) v.load(memory_order::seq_cst); // #1 v.exchange(2,memory_order::seq_cst); // #2 //thread 2: ...
Engen asked 5/9 at 9:52

3

Solved

Consider this example: #include <iostream> #include <atomic> #include <random> #include <thread> int need_close(){ random_device rd; std::mt19937 gen(rd()); uniform_int_d...
Ghostwrite asked 27/8 at 8:24

1

Solved

I want to implement a sequence lock in C++23. If possible, it should not rely on non-standard extensions or undefined behavior. There is the proposal P1478R8: Byte-wise atomic memcpy, which covers ...
Shirring asked 22/8 at 21:14

2

Solved

I need a function to atomically add float32 values in Go. This is what came up with based on some C code I found: package atomic import ( "sync/atomic" "unsafe" "math" ) func AddFloat32(addr ...
Leadership asked 15/12, 2014 at 20:14

0

Generally _Atomic does not imply semantics of volatile, i.e. operations on the atomic object are not observable side effects that the compiler needs to preserve. As a consequence the compiler can o...
Quiroz asked 22/7 at 5:33

3

Solved

I'm trying to understand memory fences in c++11, I know there are better ways to do this, atomic variables and so on, but wondered if this usage was correct. I realize that this program doesn't do ...
Horsehide asked 29/11, 2012 at 18:31

3

Solved

Consider the following code: #include <atomic> #include <thread> #include <cassert> #include <memory> int i = 0; std::atomic_int a{0}; int main() { std::thread thr1{[] {...
Calque asked 7/6 at 10:37

1

C++20 std::atomic has wait and notify_* member functions, but no wait_for/wait_until. The Microsoft STL implementation for std::atomic uses WaitOnAddress (when the OS is new enough to has it). And ...
Corinecorinna asked 21/10, 2021 at 10:20

5

Solved

Are C/C++ fundamental types, like int, double, etc., atomic, e.g. threadsafe? Are they free from data races; that is, if one thread writes to an object of such a type while another thread reads fr...
Warila asked 5/2, 2016 at 14:4

2

In the ARM documentation, it mentions that The Cortex-M4 processor supports ARMv7 unaligned accesses, and performs all accesses as single, unaligned accesses. They are converted into two or mo...
Gander asked 30/11, 2019 at 3:34

4

Solved

atomic_flag_test_and_set Yes! atomic_flag_clear Yes! atomic_flag_test_and_clear nope atomic_flag_set nope If you wanted to do something like set a flag on a event in some context, and in some oth...
Nunez asked 2/2 at 19:15

3

Solved

The standard technique to enforce atomic access to volatile variables shared with ISRs, via "atomic access guards" or "interrupt guards", in particular when running a bare metal...
Greenman asked 26/3, 2022 at 8:53

4

Solved

I would like to generate identifiers for a class named order in a thread-safe manner. The code below does not compile. I know that the atomic types do not have copy constructors, and I assume that ...
Brittenybrittingham asked 8/12, 2013 at 11:50

2

Solved

As the title says, are procedures atomic in MySQL? i.e. would something like for (..) <check_if_row_has_flag> for (..) <update_row> work atomically? Interestingly, I couldn't find mu...
Anson asked 28/6, 2018 at 23:44

7

Is a PostgreSQL function such as the following automatically transactional? CREATE OR REPLACE FUNCTION refresh_materialized_view(name) RETURNS integer AS $BODY$ DECLARE _table_name ALIAS FOR $1...
Franconian asked 8/10, 2012 at 8:48

1

Let's consider this trivial code: #include <atomic> std::atomic<int> a; void f(){ for(int k=0;k<100;++k) a.load(std::memory_order_relaxed); } MSVC, Clang and GCC all perform 10...

1

Can somebody explain the usage of WRITE_ONCE and READ_ONCE? And internally WRITE_ONCE uses a volatile qualifier. Why? How does WRITE_ONCE and READ_ONCE solve cache coherency problem? Difference bet...
Leff asked 29/5, 2018 at 17:2

4

Solved

std::array< std::atomic_size_t, 10 > A; // ... std::atomic_init(A, {0}); // error A = {ATOMIC_VAR_INIT(0)}; // error How would you initialize an array of std::atomic to 0s? Even for loops up...
Gleanings asked 17/10, 2013 at 18:50

2

Solved

In GCC atomic built-in I found that __atomic_exchange function does have a third parameter int memorder, which could take one of the values __ATOMIC_RELAXED, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE, __A...
Biller asked 3/9, 2015 at 13:36

0

In this comment under a CWG issue, Jens Maurer says The read-compare-write is a single, indivisible operation ("atomically"). However, as discussed in For purposes of ordering, is ato...
Sands asked 23/10, 2023 at 7:59

1

I wanted to replace the pthread_spinlock_t example with my own spinlock implementation. However, my implementation's result is literally far lower than the pthread_spinlock_t performance. While the...
Pilcomayo asked 2/10, 2023 at 17:3

4

Solved

While porting some Windows C++ code to iOS, I need to provide an implementation of Win32's long InterlockedIncrement(long *p) call. This is easy enough using the functions defined in <libkern/OS...
Brena asked 15/11, 2012 at 14:16

© 2022 - 2024 — McMap. All rights reserved.