2
#include <iostream> #include <thread> class A{ public: static thread_local long l ; int value ; A(int a = 2){ value = a; } int foo(){ A::l = 3; return 3; } }; A a; int main(){ // A::l = 3; a.foo(); return 0; } 

above code on compiling gives error can someone help to resolve them? when i remove the reads and writes to static thread_local this seems to compile . does it needs some special libraries or linkers to work properly . I need to keep static thread_local to get same features as threadLocal class in java

4
  • 2
    #include <iostream> #include <thread> class A{ public: inline static thread_local long l ; int value ; A(int a = 2){ value = a; } int foo(){ A::l = 3; return 3; } }; A a; int main(){ A::l = 3; a.foo(); return 0; } Commented Mar 8, 2019 at 18:55
  • 1
    thanks a lot adding inline with g++ -std=c++17 solved problem Commented Mar 8, 2019 at 18:56
  • 2
    inline keyword worked for me, but why? Commented Dec 12, 2020 at 8:16
  • @Rajeshkumar I have the same question, how does inline keyword do the magic? Commented Feb 27, 2023 at 9:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.