gpt4 book ai didi

c++ - 释放锁定时持有互斥锁的对象

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:12 24 4
gpt4 key购买 nike

我遇到了存储在实例中的互斥锁的问题。举个例子,我这​​样写:

#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include "Sleep.h"

struct Test
{
std::shared_mutex mutex;
};

Test* test = new Test();

void t1()
{
std::unique_lock<std::shared_mutex> lock(test->mutex);
SLEEP(200);
delete test;
std::cout << "thread 1" << std::endl;
}

void t2()
{
SLEEP(100);
std::cout << "hold" << std::endl;
std::shared_lock<std::shared_mutex> lock(test->mutex);
std::cout << "thread 2" << std::endl;
}

int main()
{
std::thread trd1 = std::thread(&t1);
std::thread trd2 = std::thread(&t2);
trd1.join();
trd2.join();

std::cin.get();
return 0;
}

我想要的是,一旦 test 被删除,互斥锁 reference(?) shared_lock 就会解锁。目标是使多个线程使用的对象的删除成为线程安全的。至于 shared_lock 之后发生的事情并不重要(我知道我不能再在那里使用 test 了)。

输出是:

hold
thread 1
(here should be 'thread 2')

但不幸的是,t2 似乎陷入僵局。

问题

  1. 有没有办法让 shared_lockunique_lock 超出范围后继续? (使用实例的互斥量很重要)
  2. 在测试时,我还尝试用 shared_timed_mutex 替换 shared_mutex。令我惊讶的是,它会在 t1() 结束时导致崩溃。这是为什么?

最佳答案

当锁仍在使用互斥锁时,您通过删除 unique_lock 持有的互斥锁来调用未定义行为。

根据语言标准,unique_lock 类的描述(§ 30.4.2.2):

The behavior of a program is undefined if [the mutex held by the lock] does not exist for the entire remaining lifetime of the unique_lock object.

在销毁互斥锁​​(或包含互斥锁的类)之前,您必须销毁或释放锁。

关于c++ - 释放锁定时持有互斥锁的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40029838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com