Can you use thread local variables inside a class or structure

后端 未结 9 1843
名媛妹妹
名媛妹妹 2020-12-30 23:34

Like this.

struct some_struct
{
 // Other fields
 .....
 __thread int tl;
}

I\'m trying to do that but the compiler is giving me this error

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 00:18

    Write this way:

    template  struct S {
        thread_local static int tlm;
    };
    template <> thread_local int S::tlm = 0; // "static" does not appear here
    

    as stated in https://en.cppreference.com/w/cpp/language/storage_duration

提交回复
热议问题