Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boost shared_ptr <XXX> thread safe?

I have a question about boost::shared_ptr<T>.

There are lots of thread.

using namespace boost;  class CResource {   // xxxxxx }  class CResourceBase { public:    void SetResource(shared_ptr<CResource> res)    {      m_Res = res;    }     shared_ptr<CResource> GetResource()    {       return m_Res;    } private:    shared_ptr<CResource> m_Res; }  CResourceBase base;  //---------------------------------------------- // Thread_A:     while (true)     {        //...        shared_ptr<CResource> nowResource = base.GetResource();        nowResource.doSomeThing();        //...     }  // Thread_B:     shared_ptr<CResource> nowResource;     base.SetResource(nowResource);     //... 

Q1

If Thread_A do not care the nowResource is the newest, will this part of code have problem?

I mean when Thread_B do not SetResource() completely, Thread_A get a wrong smart point by GetResource()?

Q2

What does thread-safe mean?

If I do not care about whether the resource is newest, will the shared_ptr<CResource> nowResource crash the program when the nowResource is released or will the problem destroy the shared_ptr<CResource>?

like image 787
user25749 Avatar asked Mar 28 '09 08:03

user25749


People also ask

Is shared_ptr copy thread safe?

When you copy a std::shared_ptr in a thread, all is fine. At first to (2). By using copy construction for the std::shared_ptr localPtr, only the control block is used. That is thread-safe.

What is Boost :: shared_ptr?

shared_ptr is now part of the C++11 Standard, as std::shared_ptr . Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamically allocated array. This is accomplished by using an array type ( T[] or T[N] ) as the template parameter.

Are unique pointers thread safe?

unique_ptr is thread safe when used correctly. You broke the unwritten rule: Thou shalt never pass unique_ptr between threads by reference. The philosophy behind unique_ptr is that it has a single (unique) owner at all times.

What is Boost :: Make_shared?

The header file <boost/make_shared. hpp> provides a family of overloaded function templates, make_shared and allocate_shared , to address this need. make_shared uses the global operator new to allocate memory, whereas allocate_shared uses an user-supplied allocator, allowing finer control.


1 Answers

boost::shared_ptr<> offers a certain level of thread safety. The reference count is manipulated in a thread safe manner (unless you configure boost to disable threading support).

So you can copy a shared_ptr around and the ref_count is maintained correctly. What you cannot do safely in multiple threads is modify the actual shared_ptr object instance itself from multiple threads (such as calling reset() on it from multiple threads). So your usage is not safe - you're modifying the actual shared_ptr instance in multiple threads - you'll need to have your own protection.

In my code, shared_ptr's are generally locals or parameters passed by value, so there's no issue. Getting them from one thread to another I generally use a thread-safe queue.

Of course none of this addresses the thread safety of accessing the object pointed to by the shared_ptr - that's also up to you.

like image 50
Michael Burr Avatar answered Sep 16 '22 20:09

Michael Burr