Is it a good way to keep thread-safety to synchronize the object?

  atomic, c++, locking, multithreading, shared-ptr

According to shared_ptr/atomic, we know that shared_ptr::reset() is thread-safe.
To prevent the object being accessed by other threads from being released. I create a store ptr to preserve the expired object.
So, the following codes are thread safe in any of time?

class data_info
{
    shared_ptr<sample> using;
    shared_ptr<sample> store;
}

// function1 and function2 are used in different threads;
void function1(data_info &a)
{
    a.using->do_something();
}

void function2(data_info &a)
{
    a.store = a.using;
    a.using.reset(new sample());
}

Source: Windows Questions C++

LEAVE A COMMENT