Question

Is is possible in Boost threading to do something like this:

std::string key;
MyClass value;    
myThread->setData(key, value);    
MyClass retrievedValue = myThread->getData(key);

setData and getData are only imaginary methods to explain my need.


EDIT : I got the necessary answer by @SLaks. But for clarification : the purpose of the question was not "a method by method" equivalent, but it was a global equivalent by which we can set somewhere in the thread by a way something that we can get it somewhere else it by a way in the same thread... :)

Thank you!

Was it helpful?

Solution

You're looking for thread-local storage.

Use boost:thread_specific_ptr.

OTHER TIPS

Not as such, no.

It is possible though, to write your own thread class (implemented in terms of std::thread or boost::thread or something else), that also holds a map of values (besides the thread instance), and that does what you are asking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top