Commit 8ddb61d6ea85b17d92c6b6e9ce5ebc84f8f04836

Authored by Wiebe Cazemier
1 parent 260643dc

Fix race condition in thread creation

mainapp.cpp
... ... @@ -252,8 +252,7 @@ void MainApp::start()
252 252 for (int i = 0; i < NR_OF_THREADS; i++)
253 253 {
254 254 std::shared_ptr<ThreadData> t(new ThreadData(i, subscriptionStore, *confFileParser.get()));
255   - std::thread thread(do_thread_work, t.get());
256   - t->moveThreadHere(std::move(thread));
  255 + t->start(&do_thread_work);
257 256 threads.push_back(t);
258 257 }
259 258  
... ...
threaddata.cpp
... ... @@ -13,9 +13,9 @@ ThreadData::ThreadData(int threadnr, std::shared_ptr&lt;SubscriptionStore&gt; &amp;subscri
13 13 epollfd = check<std::runtime_error>(epoll_create(999));
14 14 }
15 15  
16   -void ThreadData::moveThreadHere(std::thread &&thread)
  16 +void ThreadData::start(thread_f f)
17 17 {
18   - this->thread = std::move(thread);
  18 + this->thread = std::thread(f, this);
19 19  
20 20 pthread_t native = this->thread.native_handle();
21 21 std::ostringstream threadName;
... ...
threaddata.h
... ... @@ -20,6 +20,8 @@
20 20 #include "authplugin.h"
21 21 #include "logger.h"
22 22  
  23 +typedef void (*thread_f)(ThreadData *);
  24 +
23 25 class ThreadData
24 26 {
25 27 std::unordered_map<int, Client_p> clients_by_fd;
... ... @@ -39,7 +41,7 @@ public:
39 41 ThreadData(const ThreadData &other) = delete;
40 42 ThreadData(ThreadData &&other) = delete;
41 43  
42   - void moveThreadHere(std::thread &&thread);
  44 + void start(thread_f f);
43 45 void quit();
44 46 void giveClient(Client_p client);
45 47 Client_p getClient(int fd);
... ...