I've read that
"Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership." (MSDN: Thread Safety in the Standard C++ Library)
Does that mean that changing shared_ptr object is safe ?
For an instance, is the next code considered safe:
shared_ptr<myClass> global = make_shared<myClass>();
...
//In thread 1
shared_ptr<myClass> private = global;
...
//In thread 2
global = make_shared<myClass>();
...
Can I be sure in that case that thread 1 private
will have the original value of global
or the new value which thread 2 assigned but either way it will have a valid shared_ptr to myClass?
==EDIT==
Just to explain my motivation. I want to have a shared pointer to hold my configuration and I have a thread pool to handle requests.
so global
is the global configuration.
thread 1
is taking the current configuration as it start to handle a request.
thread 2
is updating the configuration. (only apply to future requests)
If it's work, I can update the configuration that way without breaking it in the middle of a request handling.
What you're reading isn't meaning what you think it means. First of all, try the msdn page for shared_ptr itself.
Scroll down into the "Remarks" section and you'll get to the meat of the issue. Basically, a shared_ptr<>
points to a "control block" which is how it keeps track of how many shared_ptr<>
objects are actually pointing to the "Real" object. So when you do this:
shared_ptr<int> ptr1 = make_shared<int>();
While there is only 1 call to allocate memory here via make_shared
, there are two "logical" blocks that you should not treat the same. One is the int
which stores the actual value, and the other is the control block, which stores all the shared_ptr<>
"magic" that makes it work.
It is only the control block itself which is thread-safe.
I put that on its own line for emphasis. The contents of the shared_ptr
are not thread-safe, nor is writing to the same shared_ptr
instance. Here's something to demonstrate what I mean:
// In main()
shared_ptr<myClass> global_instance = make_shared<myClass>();
// (launch all other threads AFTER global_instance is fully constructed)
//In thread 1
shared_ptr<myClass> local_instance = global_instance;
This is fine, in fact you can do this in all threads as much as you want. And then when local_instance
is destructed (by going out of scope), it is also thread-safe. Somebody can be accessing global_instance
and it won't make a difference. The snippet you pulled from msdn basically means "access to the control block is thread-safe" so other shared_ptr<>
instances can be created and destroyed on different threads as much as necessary.
//In thread 1
local_instance = make_shared<myClass>();
This is fine. It will affect the global_instance
object, but only indirectly. The control block it points to will be decremented, but done in a thread-safe way. local_instance
will no longer point to the same object (or control block) as global_instance
does.
//In thread 2
global_instance = make_shared<myClass>();
This is almost certainly not fine if global_instance
is accessed from any other threads (which you say you're doing). It needs a lock if you're doing this because you're writing to wherever global_instance
lives, not just reading from it. So writing to an object from multiple threads is bad unless it's you have guarded it through a lock. So you can read from global_instance
the object by assigning new shared_ptr<>
objects from it but you can't write to it.
// In thread 3
*global_instance = 3;
int a = *global_instance;
// In thread 4
*global_instance = 7;
The value of a
is undefined. It might be 7, or it might be 3, or it might be anything else as well. The thread-safety of the shared_ptr<>
instances only applies to managing shared_ptr<>
instances which were initialized from each other, not what they're pointing to.
To emphasize what I mean, look at this:
shared_ptr<int> global_instance = make_shared<int>(0);
void thread_fcn();
int main(int argc, char** argv)
{
thread thread1(thread_fcn);
thread thread2(thread_fcn);
...
thread thread10(thread_fcn);
chrono::milliseconds duration(10000);
this_thread::sleep_for(duration);
return;
}
void thread_fcn()
{
// This is thread-safe and will work fine, though it's useless. Many
// short-lived pointers will be created and destroyed.
for(int i = 0; i < 10000; i++)
{
shared_ptr<int> temp = global_instance;
}
// This is not thread-safe. While all the threads are the same, the
// "final" value of this is almost certainly NOT going to be
// number_of_threads*10000 = 100,000. It'll be something else.
for(int i = 0; i < 10000; i++)
{
*global_instance = *global_instance + 1;
}
}
A shared_ptr<>
is a mechanism to ensure that multiple object owners ensure an object is destructed, not a mechanism to ensure multiple threads can access an object correctly. You still need a separate synchronization mechanism to use it safely in multiple threads (like std::mutex).
The best way to think about it IMO is that shared_ptr<>
makes sure that multiple copies pointing to the same memory don't have synchronization issues for itself, but doesn't do anything for the object pointed to. Treat it like that.