Fixed non-Qt thread-local storage implementation: needs to reuse holder object when using 'setLocalData'

This commit is contained in:
Matthias Koefferlein 2022-05-29 19:33:30 +02:00
parent f2b91997f0
commit baa92d4455
2 changed files with 6 additions and 3 deletions

View File

@ -228,7 +228,6 @@ protected:
} }
}; };
// TODO: this is the non-threaded dummy implementation
template <class T> template <class T>
class ThreadStorage class ThreadStorage
: public ThreadStorageBase : public ThreadStorageBase
@ -253,7 +252,11 @@ public:
void setLocalData (const T &data) void setLocalData (const T &data)
{ {
add (new ThreadStorageHolder<T> (new T (data))); if (hasLocalData ()) {
localData () = data;
} else {
add (new ThreadStorageHolder<T> (new T (data)));
}
} }
}; };

View File

@ -229,7 +229,7 @@ TEST(3)
MyThread3 my_thread; MyThread3 my_thread;
my_thread.start (); my_thread.start ();
// While we start the loop inside the thread we run it outside. Since // 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. // A mutex is not involved.
EXPECT_EQ (my_thread.do_run (9999999), 9999999); EXPECT_EQ (my_thread.do_run (9999999), 9999999);
my_thread.wait (); my_thread.wait ();