Problem
c++11 has introduced thread_local
which provides thread local data, but cannot be used with non static data members.
This leads to the question:
Why may thread_local not be applied to non-static data members and how to implement thread-local non-static data members?
And the answer:
... I suggest using
boost::thread_specific_ptr
. You can put one of these inside your class and get the behavior you want.
But the boost::thread_specific_ptr
destructor has the following note attached:
All the thread specific instances associated to this thread_specific_ptr (except maybe the one associated to this thread) must be null.
Is there a way to work around this?
I need thread local storage for non-static data members, which will free all thread-data on destruction, even if there are still Threads running (or a tls which at least doesn't fail/leak on destruction).
If boost::thread_specific_ptr
is not the right choice for this, could I use a mutex protected std::vector
instead?
Background
I have a threadsafe class which receives data from mongodb.
class JsonTable
{
public:
std::string getData() const;
//....
private:
ThreadLocalStorage<mongocxx::client> _clients;
//....
};
mongocxx::client
must not be shared across multiple threads.
Thus in order to make getData
Thread-safe, I need to construct a mongocxx::client
per Thread. And when my JsonTable
class is destructed I would like all clients to be closed/destructed even if the threads which initially created them are still running.