August 2017
Intermediate to advanced
438 pages
14h 19m
English
It is time to make a thread local variable. Declare a function in a header file after the connection class definition:
connection& get_connection();
Make your source file look like this:
#include <boost/thread/tss.hpp>boost::thread_specific_ptr<connection> connection_ptr;connection& get_connection() { connection* p = connection_ptr.get(); if (!p) { connection_ptr.reset(new connection); p = connection_ptr.get(); p->open(); } return *p;}
Done. Using a thread-specific resource was never so easy:
void task() { int result; // Some computations go there. // ... // Sending the result: get_connection().send_result(result);}
Read now
Unlock full access