Share a pointer safely with C++20 std::atomic of std::shared_ptr
Swap a shared_ptr between threads without a data race using C++20 std::atomic<std::shared_ptr<T>>, supported by libstdc++ since GCC 12.
Short posts that solve one thing: the code, the output we got, and why it works.
Swap a shared_ptr between threads without a data race using C++20 std::atomic<std::shared_ptr<T>>, supported by libstdc++ since GCC 12.
Push onto a lock-free stack with a compare_exchange_weak loop in C++17, where a failed CAS refreshes the node's next pointer for the retry.
Wake a single waiter with pthread_cond_signal after changing shared state under the mutex, and recheck the predicate in a loop on glibc 2.39.
Define a file-scope kernel mutex with DEFINE_MUTEX(name), which needs no mutex_init() call and is ready before module init runs, on Linux 6.x.
Initialise a global pthread mutex at load time with PTHREAD_MUTEX_INITIALIZER in glibc 2.39, with no init call and no destroy for a static mutex.
Hand data from one thread to another with a release store and an acquire load on a std::atomic flag, the pairing that creates happens-before.
Read the running core with xPortGetCoreID in an ESP-IDF 5 task pinned with xTaskCreatePinnedToCore, instead of assuming core 0.
Increment a shared counter from several threads with C11 atomic_fetch_add_explicit and memory_order_relaxed, which also returns the old value.
Create and wake a kernel thread in one call with kthread_run(), check the ERR_PTR return, and give it a name that shows up in ps on Linux 6.x.
pthread_once() runs an init function exactly once across all threads in glibc 2.39, and every caller waits until it has finished.
Use pthread_self() in glibc 2.39 to name the calling thread or detach it, and never print the opaque pthread_t as if it were an integer.
Compile with -fsanitize=thread -g and run your tests to get a ThreadSanitizer report for every unsynchronised access, using GCC 14 or Clang 19.
We send one email when a new post is published. No spam.