同步对象是保持线程安全的好方法吗



根据shared_ptr/atomic,我们知道shared_ptr::reset()是线程安全的。防止其他线程访问的对象被释放。我创建了一个store ptr来保存过期的对象。那么,以下代码在任何时候都是线程安全的?

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());
}

我写了一个演示来确认我的想法

#include <bits/stdc++.h>
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
using namespace std;
#define ATOMIC_READLOCK(stored_data_ptr) 
    {                                    
        auto atomic_readlock_##name = stored_data_ptr;
#define ATOMIC_READLOCK_RELEASE() 
    }
#define ATOMIC_WRITELOCK(stored_data_ptr, new_data_ptr) 
    stored_data_ptr = new_data_ptr
class bar
{
public:
    bar() : num(1) {}
    explicit bar(int x) : num(x) {}
    int get_num()
    {
        return num;
    }
    void set_num(int x)
    {
        num = x;
    }
private:
    int num;
};
vector<int> v;
shared_ptr<bar> ptr_1, ptr_2;
void get_func()
{
    for (int i = 0; i < 100000; i++)
    {
        ATOMIC_READLOCK(ptr_1);
        v.push_back(ptr_1->get_num());
        ATOMIC_READLOCK_RELEASE();
    }
}
void set_func()
{
    for (int i = 0; i < 100000; i++)
    {
        ptr_2 = make_shared<bar>(i);
        ATOMIC_WRITELOCK(ptr_1, ptr_2);
    }
}
int main()
{
    std::thread t1(get_func);
    std::thread t2(set_func);
    t1.join();
    t2.join();
}

但它发生了核心转储

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./reset'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000555a2a075c58 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
[Current thread is 1 (Thread 0x7f70244ec700 (LWP 30265))]
(gdb) where
#0  0x0000555a2a075c58 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
#1  0x0000555a2a0758b9 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() ()
#2  0x0000555a2a075790 in std::__shared_ptr<bar, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() ()
#3  0x0000555a2a0757c2 in std::shared_ptr<bar>::~shared_ptr() ()
#4  0x0000555a2a0753cc in get_func() ()
#5  0x0000555a2a076069 in void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) ()
#6  0x0000555a2a075b3e in std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) ()
#7  0x0000555a2a077b52 in decltype (__invoke((_S_declval<0ul>)())) std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) ()
#8  0x0000555a2a077af0 in std::thread::_Invoker<std::tuple<void (*)()> >::operator()() ()
#9  0x0000555a2a077a3c in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() ()
#10 0x00007f70251706df in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#11 0x00007f7024c836db in start_thread (arg=0x7f70244ec700) at pthread_create.c:463
#12 0x00007f70249aca3f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

我认为您的代码是线程安全的。但我也提出了另一种解决方案,访问类成员的线程将获得shared_ptr的所有权。它本质上使整个场景线程安全。

void function1(data_info &a) {
    auto member = a.member;
    member->do_something();
}
void function2(data_info &a) {
    a.member = std::make_shared<sample>();
}

但是您将如何删除保留shared_ptr。当CCD_ 5在某个地方被调用时,reserve可能正好超出范围
要做这些事情,您需要weak_ptr。https://stackoverflow.com/questions/63291791/does-weak-ptrs-lock-will-always-work-in-statement-instore-com-r-lock-i?noredirect=1#comment111921096_63291791

upd:是的,看起来是安全的

更新2:所以我们这里有:
对于weak_ptr.lock(),其shared_ptr的ref计数器增益+1,而lock()的填充尚未完成(请参阅链接(
对于shared_ptr->,它不是。刚才已经测试过了。

所以它不是线程安全的,也不会工作
要查看它,dosmth()应该比所有其他程序运行更长时间,并使用this

相关内容

  • 没有找到相关文章

最新更新