|
Boost Users : |
Subject: Re: [Boost-users] thread safety of shared_ptr
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2012-04-18 02:18:12
2012/4/18 Yuanlong Shao <shaoyl85_at_[hidden]>
> Hi all,
>
> If I do the following
>
> ----------------------------------------------------------------------------------------------------------
> shared_ptr<int> pA( new int(1) );
> shared_ptr<int> pB( new int(2) );
> shared_ptr<int> pNew( new int(3) );
>
> // thread A
> pA = pB;
>
> // thread B
> pB = pNew;
>
> ----------------------------------------------------------------------------------------------------------
>
> I know that whether pA will be int(2) or int(3) after execution is
> undefined, but if I do want to do this no matter what value of
> pA I get, will this cause atomicity problem? I mean, thread A
> increases counter for int(2), but thread B decrease it, is this
> thread safe?
No, it's not thread safe. It's not allowed to read a shared_ptr while
another thread simultaneously modifies the same object. See
http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety
.
Roman Perepelitsa.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net