Hi,
what are the member functions for thread_local. I am trying to compare with boost::thread_specific_ptr.
How do I know whether data is already exist or not. I didn't find any isSet(), setValue(), getValue(), or reset() for thread_local. In some examples, synchronizing thread using lock before reading thread_local variable.
54 #include <iostream>
55 #include <string>
56 #include <thread>
57 #include <mutex>
58
59
62
63 class testcls
64 {
65 public:
66 void write(int x)
67 {
68 l = x;
69 }
70 void read()
71 {
72 std::cout << "val: " << l << std::endl;
73 }
74
75 private:
76 int l;
77 };
78 typedef std::shared_ptr<testcls> testclsptr;
79 static thread_local testclsptr threadVaribale;
int main()
{
threadVaribale = std::shared_ptr<testcls> (new testcls);
}
void thr1()
{
threadVaribale->write(5);
}
void thr2()
{
//Do I need to add mutex before read? this is tls variable.
threadVaribale->read();
}