atomic-swap

GLIB: g_atomic_int_get becomes NO-OP?

£可爱£侵袭症+ 提交于 2020-01-02 08:24:25
问题 In a larger piece of code, I noticed that the g_atomic_* functions in glib were not doing what I expected, so I wrote this simple example: #include <stdlib.h> #include "glib.h" #include "pthread.h" #include "stdio.h" void *set_foo(void *ptr) { g_atomic_int_set(((int*)ptr), 42); return NULL; } int main(void) { int foo = 0; pthread_t other; if (pthread_create(&other, NULL, set_foo, &foo)== 0) { pthread_join(other, NULL); printf("Got %d\n", g_atomic_int_get(&foo)); } else { printf("Thread did

Is there atomic increment with the check preconditions, that the atomic value was less than the specified value?

喜你入骨 提交于 2019-12-22 09:49:17
问题 Is in the new standard C++ atomic increment operation with the check preconditions before the incremented the value, that the atomic value was less than the specified value? Can I do it easier and faster than the following code? int atomic_inc(std::atomic_int& val, int less_than) { int new_val; int old_val = val.load(); do { if (old_val > less_than) return old_val; new_val = old_val + 1; } while (!val.compare_exchange_weak(old_val, new_val)); return new_val; } If somebody don't know how works

Is there atomic increment with the check preconditions, that the atomic value was less than the specified value?

送分小仙女□ 提交于 2019-12-05 18:59:56
Is in the new standard C++ atomic increment operation with the check preconditions before the incremented the value, that the atomic value was less than the specified value? Can I do it easier and faster than the following code? int atomic_inc(std::atomic_int& val, int less_than) { int new_val; int old_val = val.load(); do { if (old_val > less_than) return old_val; new_val = old_val + 1; } while (!val.compare_exchange_weak(old_val, new_val)); return new_val; } If somebody don't know how works compare_exchange_weak: compare_exchange_weak reads val, compares with old_val, and if they are not

Lock-free swap of two unique_ptr<T>

无人久伴 提交于 2019-12-03 04:07:48
问题 Swapping two unique_ptr s is not guaranteed to be threadsafe. std::unique_ptr<T> a, b; std::swap(a, b); // not threadsafe Since I need atomic pointer swaps and since I like the ownership handling of unique_ptr , is there a simple way to combine them both? Edit: If this is not possible, I am open for alternatives. I at least want to do something like this: threadshared_unique_ptr<T> global; void f() { threadlocal_unique_ptr<T> local(new T(...)); local.swap_content(global); // atomically for

Lock-free swap of two unique_ptr<T>

為{幸葍}努か 提交于 2019-12-02 17:26:55
Swapping two unique_ptr s is not guaranteed to be threadsafe. std::unique_ptr<T> a, b; std::swap(a, b); // not threadsafe Since I need atomic pointer swaps and since I like the ownership handling of unique_ptr , is there a simple way to combine them both? Edit: If this is not possible, I am open for alternatives. I at least want to do something like this: threadshared_unique_ptr<T> global; void f() { threadlocal_unique_ptr<T> local(new T(...)); local.swap_content(global); // atomically for global } What is the idiomatic way of doing this in C++11? Stas Lock-free swapping of two pointers It

Atomic swap in GNU C++

夙愿已清 提交于 2019-11-28 21:21:46
I want to verify that my understanding is correct. This kind of thing is tricky so I'm almost sure I am missing something. I have a program consisting of a real-time thread and a non-real-time thread. I want the non-RT thread to be able to swap a pointer to memory that is used by the RT thread. From the docs, my understanding is that this can be accomplished in g++ with: // global Data *rt_data; Data *swap_data(Data *new_data) { #ifdef __GNUC__ // Atomic pointer swap. Data *old_d = __sync_lock_test_and_set(&rt_data, new_data); #else // Non-atomic, cross your fingers. Data *old_d = rt_data; rt

Atomic swap in GNU C++

随声附和 提交于 2019-11-27 13:45:42
问题 I want to verify that my understanding is correct. This kind of thing is tricky so I'm almost sure I am missing something. I have a program consisting of a real-time thread and a non-real-time thread. I want the non-RT thread to be able to swap a pointer to memory that is used by the RT thread. From the docs, my understanding is that this can be accomplished in g++ with: // global Data *rt_data; Data *swap_data(Data *new_data) { #ifdef __GNUC__ // Atomic pointer swap. Data *old_d = __sync