#include#include boost::thread_specific_ptr ptr; int main() { ptr.reset(new int(5)); std::cout << *ptr << std::endl; // prints 5 ptr.reset(0); // reset the value of ptr std::cout << *ptr << std::endl; // prints 0 return 0; }
#includeBrief Explanation Example 1: In this example, we create a thread-specific pointer that stores an integer value. The reset() method is called to change the value of the pointer. The output from std::cout shows that the value is set to 5, and then reset to 0. Example 2: Here, we have two threads that perform the same function, but each thread's function will call the thread-specific pointer once. If the thread-specific pointer is not set, it's set to 0. Then, the value of the pointer is incremented by 1 in each thread, and the output is printed. Since each thread has its unique value, the output will show different values for each thread. Package Library: The CPP library used in the above examples is `boost::thread` from the Boost C++ Libraries. Boost is a collection of free, open-source libraries that extend the C++ language, offering developers functionalities such as concurrency, networking, and data structures, etc.#include boost::thread_specific_ptr ptr; void thread_func() { if (!ptr.get()) ptr.reset(new int(0)); (*ptr)++; std::cout << *ptr << std::endl; } int main() { boost::thread t1(thread_func); boost::thread t2(thread_func); t1.join(); t2.join(); return 0; }