Search code examples
c++boostshared-ptr

shared boost::shared_ptr<> variable is thread safe?


boost::shared_ptr<A> g_a;

void func1(boost::shared_ptr<A> v)
{
    g_a = v;
}

void func2()
{
    boost::shared_ptr<A> a = g_a;
    // a is good?
}

When func1() and func2() is executed from different threads, a of func2() is safe?


Solution

  • No. There is data race. One thread writes g_a, another thread reads g_a. Sync needed.