How to allocate thread local storage?

前端 未结 9 1311
夕颜
夕颜 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:36

    #include 
    static boost::thread_specific_ptr< MyClass> instance;
    if( ! instance.get() ) {
        // first time called by this thread
        // construct test element to be used in all subsequent calls from this thread
        instance.reset( new MyClass);
    }
        instance->doSomething();
    

提交回复
热议问题