From 2bd106ec0da52e7fcf616d7b3cb20d570c1a5c50 Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Tue, 22 Oct 2019 11:39:20 -0400 Subject: [PATCH] common/thread: Fix race condition in make_named_thread The thread may well no longer exist by the time we try to set the name, so have the thread set its own name first thing. Thanks to Ilya Dryomov for pointing it out. Signed-off-by: Adam C. Emerson --- src/common/Thread.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/common/Thread.h b/src/common/Thread.h index bc32755c30e..0ab65fca52f 100644 --- a/src/common/Thread.h +++ b/src/common/Thread.h @@ -16,6 +16,8 @@ #ifndef CEPH_THREAD_H #define CEPH_THREAD_H +#include +#include #include #include @@ -68,13 +70,14 @@ std::string get_thread_name(const std::thread& t); void kill(std::thread& t, int signal); template -std::thread make_named_thread(const std::string& s, +std::thread make_named_thread(std::string_view n, Fun&& fun, Args&& ...args) { - auto t = std::thread(std::forward(fun), - std::forward(args)...); - set_thread_name(t, s); - return t; -} + return std::thread([n = std::string(n)](auto&& fun, auto&& ...args) { + ceph_pthread_setname(pthread_self(), n.data()); + std::invoke(std::forward(fun), + std::forward(args)...); + }, std::forward(fun), std::forward(args)...); +} #endif -- 2.39.5