多线程访问共享对象的安全问题

多线程在访问共享对象时存在安全问题:某线程在访问共享对象时,共享对象可能已经被其它线程析构了,此时访问的共享对象是空的,造成线程的安全问题。

#include <iostream>
#include <memory>
#include <thread>

using namespace std;

class A
{
public:
    A() { cout << "A()" << endl; }
    ~A() { cout << "~A()" << endl; }
    void testA() { cout << "testA()" << endl; }
};


void handler(A *q)
{
    std::this_thread::sleep_for(std::chrono::seconds(2)); // 当前线程睡眠2s
    // q在访问A对象的时,对象A可能已经被析构
    q->testA();
}

int main()
{
    A *p = new A();
    thread t(handler, p);

    delete p; // 主线程析构p对象

    // 阻塞等待子线程结束
    t.join();

    return 0;
}
  • 以上代码中,使用裸指针p指向对象A,并启动线程t,其中A作为线程函数的参数,线程函数睡眠2s,期间主线程可能已经析构了p指针管理的对象A,故子线程在访问A对象时,存在安全问题。
  • 可以利用智能指针来解决多线程访问共享对象时的安全问题。(定义对象时用强智能指针;引用对象的地方使用弱智能指针
#include <iostream>
#include <memory>
#include <thread>

using namespace std;

class A
{
public:
    A() { cout << "A()" << endl; }
    ~A() { cout << "~A()" << endl; }
    void testA() { cout << "testA()" << endl; }
};


// void handler(A *q)
void handler(weak_ptr<A> pw) // 弱智能指针会引起对象的引用计数改变!
{
    // std::this_thread::sleep_for(std::chrono::seconds(2));
    shared_ptr<A> sp = pw.lock();
    if (sp != nullptr) // 检测共享对象是否存活
    {
        sp->testA();
    }
    else
    {
        cout << "A 对象已经析构,不能再访问.." << endl;
    }
}

int main()
{
    // A *p = new A();
    shared_ptr<A> p(new A());

    thread t(handler, weak_ptr<A>(p));

    t.detach(); // 分离线程

    std::this_thread::sleep_for(std::chrono::seconds(2)); // 主线程睡眠2s

    return 0;
}
  • 以上代码中,基于智能指针判断共享对象是否存活,解决了多线程访问共享对象时的安全问题。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值