Is boost shared_ptr <XXX> thread safe?

Posted by sxingfeng on Stack Overflow See other posts from Stack Overflow or by sxingfeng
Published on 2009-03-28T08:21:16Z Indexed on 2010/06/15 2:52 UTC
Read the original article Hit count: 240

Filed under:
|
|
|
|

I have a question about boost :: shared_ptr.

There are lots of thread.

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);
    ...

//-----------------------------------------------------------

If thread A do not care the nowResource is the newest . Will this part of code have problem? I mean when ThreadB do not SetResource completely, Thread A get a wrong smart point by GetResource?

Another question : what does thread-safe mean? If I do not care about whether the resource is newest, will the shared_ptr nowResource crash the program when the nowResource is released or will the problem destroy the shared_point?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost