c00000fd
c00000fd

Reputation: 22255

Thread-local-storage (__thread) used on a static class member

I'm trying thread-local-storage with a C++ class:

struct TestTLS
{
    TestTLS()
    {
        TestTLS::g_tls_val = 1;
    }
    
    int getTls()
    {
        return TestTLS::g_tls_val;
    }
    
    void setTls(int v)
    {
        TestTLS::g_tls_val = v;
    }
    
    
private:
    static __thread int g_tls_val;
    
};

and then:

TestTLS tls;
int ff = tls.getTls();
tls.setTls(3);
int ee = tls.getTls();

But I'm getting the following errors:

Undefined symbols for architecture arm64:
  "TestTLS::g_tls_val", referenced from:
      TestTLS::getTls() in main.o
      TestTLS::setTls(int) in main.o
      TestTLS::TestTLS() in main.o

What am I doing wrong there?

Upvotes: 0

Views: 21

Answers (0)

Related Questions