From 8ddb61d6ea85b17d92c6b6e9ce5ebc84f8f04836 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sat, 9 Jan 2021 12:32:48 +0100 Subject: [PATCH] Fix race condition in thread creation --- mainapp.cpp | 3 +-- threaddata.cpp | 4 ++-- threaddata.h | 4 +++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mainapp.cpp b/mainapp.cpp index f7d02f9..d2f595c 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -252,8 +252,7 @@ void MainApp::start() for (int i = 0; i < NR_OF_THREADS; i++) { std::shared_ptr t(new ThreadData(i, subscriptionStore, *confFileParser.get())); - std::thread thread(do_thread_work, t.get()); - t->moveThreadHere(std::move(thread)); + t->start(&do_thread_work); threads.push_back(t); } diff --git a/threaddata.cpp b/threaddata.cpp index 6d8041c..5816633 100644 --- a/threaddata.cpp +++ b/threaddata.cpp @@ -13,9 +13,9 @@ ThreadData::ThreadData(int threadnr, std::shared_ptr &subscri epollfd = check(epoll_create(999)); } -void ThreadData::moveThreadHere(std::thread &&thread) +void ThreadData::start(thread_f f) { - this->thread = std::move(thread); + this->thread = std::thread(f, this); pthread_t native = this->thread.native_handle(); std::ostringstream threadName; diff --git a/threaddata.h b/threaddata.h index b9583cf..386932d 100644 --- a/threaddata.h +++ b/threaddata.h @@ -20,6 +20,8 @@ #include "authplugin.h" #include "logger.h" +typedef void (*thread_f)(ThreadData *); + class ThreadData { std::unordered_map clients_by_fd; @@ -39,7 +41,7 @@ public: ThreadData(const ThreadData &other) = delete; ThreadData(ThreadData &&other) = delete; - void moveThreadHere(std::thread &&thread); + void start(thread_f f); void quit(); void giveClient(Client_p client); Client_p getClient(int fd); -- libgit2 0.21.4