From b7e892d545bf2788bb3b7d20b7c24c69c92b5e7d Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 6 Feb 2021 13:38:05 -0800 Subject: [PATCH] next_thread_id to use atomics, not locks We have multiple places where we use std::atomic, so let's use it in next_thread_id too. --- src/iothread.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/iothread.cpp b/src/iothread.cpp index 19ed562c2..62e760e8b 100644 --- a/src/iothread.cpp +++ b/src/iothread.cpp @@ -490,9 +490,10 @@ bool make_detached_pthread(void_func_t &&func) { static uint64_t next_thread_id() { // Note 0 is an invalid thread id. - static owning_lock s_last_thread_id{}; - auto tid = s_last_thread_id.acquire(); - return ++*tid; + // Note fetch_add is a CAS which returns the value *before* the modification. + static std::atomic s_last_thread_id{}; + uint64_t res = 1 + s_last_thread_id.fetch_add(1, std::memory_order_relaxed); + return res; } uint64_t thread_id() {