From baa92d44553ba498bac64bafddfee7214629a055 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sun, 29 May 2022 19:33:30 +0200 Subject: [PATCH] Fixed non-Qt thread-local storage implementation: needs to reuse holder object when using 'setLocalData' --- src/tl/tl/tlThreads.h | 7 +++++-- src/tl/unit_tests/tlThreadsTests.cc | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tl/tl/tlThreads.h b/src/tl/tl/tlThreads.h index 1f4a7a870..2fbd7204d 100644 --- a/src/tl/tl/tlThreads.h +++ b/src/tl/tl/tlThreads.h @@ -228,7 +228,6 @@ protected: } }; -// TODO: this is the non-threaded dummy implementation template class ThreadStorage : public ThreadStorageBase @@ -253,7 +252,11 @@ public: void setLocalData (const T &data) { - add (new ThreadStorageHolder (new T (data))); + if (hasLocalData ()) { + localData () = data; + } else { + add (new ThreadStorageHolder (new T (data))); + } } }; diff --git a/src/tl/unit_tests/tlThreadsTests.cc b/src/tl/unit_tests/tlThreadsTests.cc index c5a65d4a5..57d2285a9 100644 --- a/src/tl/unit_tests/tlThreadsTests.cc +++ b/src/tl/unit_tests/tlThreadsTests.cc @@ -229,7 +229,7 @@ TEST(3) MyThread3 my_thread; my_thread.start (); // While we start the loop inside the thread we run it outside. Since - // the counter is TLS, both loops will to the same but with different data. + // the counter is TLS, both loops will do the same but with different data. // A mutex is not involved. EXPECT_EQ (my_thread.do_run (9999999), 9999999); my_thread.wait ();