How to allocate thread local storage?

前端 未结 9 1313
夕颜
夕颜 2020-12-07 10:40

I have a variable in my function that is static, but I would like it to be static on a per thread basis.

How can I allocate the memory for my C++ class such that eac

9条回答
  •  感动是毒
    2020-12-07 11:19

    boost::thread_specific_ptr is the best way as it portable solution.

    On Linux & GCC you may use __thread modifier.

    So your instance variable will look like:

    static __thread MyClass *instance = new MyClass();
    

提交回复
热议问题