boost::thread_specific_ptrthread_data; void thread_func() { // Get the thread-specific data for this thread int* data = thread_data.get(); // Create new data if none exists if (!data) { data = new int; thread_data.reset(data); } // Use data for this thread *data += 1; }
boost::thread_specific_ptrIn this example, we declare a boost::thread_specific_ptr that holds an object of type MyObject. In our thread function, we use the object_data for this thread and then delete the object when we are finished with it. We do this by calling the reset() method with no arguments, which clears the data for this thread. Overall, boost::thread_specific_ptr is a powerful tool for managing thread-local data. It is part of the Boost library, which is a set of C++ libraries that provide useful tools and algorithms for many common programming tasks.object_data; void thread_func() { // Use object_data for this thread MyObject* obj = object_data.get(); obj->do_stuff(); // Delete the object for this thread object_data.reset(); }