当前位置: 首页 > 知识库问答 >
问题:

使用lock_guard互斥锁类

梁季
2023-03-14

我已经编写了3个互斥类TMutex、TCondition和TSpinLock,它们都有一个void lock()和一个void unlock()成员。现在我想对它们使用std::lock\u-guard。我在源文件末尾为我的新互斥类安装了lock_guard,如下所示:

template class std::lock_guard<app::TMutex>;
template class std::lock_guard<app::TCondition>;
template class std::lock_guard<app::TSpinLock>;

如果我使用:

TCondition cond;
std::lock_guard<std::mutex> lock(cond);

我收到以下编译器错误消息:

.../src/inc/threads.cpp:317: 39:错误:没有匹配函数调用'std::lock_guard::lock_guard(应用程序::

我还尝试将自己的锁保护包装器实现为一个新的模板类(非常类似于原始的锁保护:

template<typename T>
class TLockGuard
{
private:
  typedef T lock_t;
  lock_t&  instance;

public:
  TLockGuard& operator=(const TLockGuard&) = delete;
  explicit TLockGuard(lock_t& F) : instance(F) { instance.lock(); }
  TLockGuard(const TLockGuard&) = delete;
  ~TLockGuard() { instance.unlock(); }
};

并对此类执行类似的实例化:

template class TLockGuard<app::TMutex>;
template class TLockGuard<app::TCondition>;
template class TLockGuard<app::TSpinLock>;

如果我像这样使用这个模板类:

TCondition cond;
std::TLockGuard<std::mutex> lock(cond);

我得到一个不同的错误:

../src/inc/threads。cpp:318:39:错误:调用“app::TLockGuard::TLockGuard(app::t条件)”时没有匹配的函数

有人能帮我理解这两种情况下都出了什么问题吗?我更喜欢使用标准的锁护,但如果我自己的包装也能工作,那就太好了。

共有1个答案

贺飞
2023-03-14

下面的例子编译的很好:

#include <thread>
#include <mutex>

struct Mutex
{
    void lock() {}
    void unlock() {}
};

struct Condition
{
    void lock() {}
    void unlock() {}
};

struct SpinLock
{
    void lock() {}
    void unlock() {}
};

template class std::lock_guard<Mutex>;
template class std::lock_guard<Condition>;
template class std::lock_guard<SpinLock>;


int main()
{
    static Mutex mutex;
    std::lock_guard<Mutex> lock(mutex);
}

你的问题是:

TCondition cond;
std::lock_guard<std::mutex> lock(cond);

您正在创建一个需要std::mutex但传递了一个TCondition的锁保护。

尝试更改为:

std::lock_guard<TCondition> lock(cond);
 类似资料:
  • Go语言包中的 sync 包提供了两种锁类型:sync.Mutex 和 sync.RWMutex。 Mutex 是最简单的一种锁类型,同时也比较暴力,当一个 goroutine 获得了 Mutex 后,其他 goroutine 就只能乖乖等到这个 goroutine 释放该 Mutex。 RWMutex 相对友好些,是经典的单写多读模型。在读锁占用的情况下,会阻止写,但不阻止读,也就是多个 gor

  • Introduction This is the fourth part of the chapter which describes synchronization primitives in the Linux kernel and in the previous parts we finished to consider different types spinlocks and semap

  • 本文向大家介绍互斥锁死锁,包括了互斥锁死锁的使用技巧和注意事项,需要的朋友参考一下 死锁可以在使用互斥锁的多线程Pthread程序中发生。让我们看看它如何发生。未锁定的互斥锁由pthread_mutex_init()函数初始化。 使用pthread_mutex_lock()和pthread_mutex_unlock()获取并释放互斥锁。如果线程尝试获取锁定的互斥锁,则对pthread_mutex_

  • 9.2. sync.Mutex互斥锁 在8.6节中,我们使用了一个buffered channel作为一个计数信号量,来保证最多只有20个goroutine会同时执行HTTP请求。同理,我们可以用一个容量只有1的channel来保证最多只有一个goroutine在同一时刻访问一个共享变量。一个只能为1和0的信号量叫做二元信号量(binary semaphore)。 gopl.io/ch9/bank

  • 问题内容: 我试图了解互斥的工作原理。到目前为止,据我了解,它可以进行原子操作并同步对某些数据的访问。 我在这里构建了一个队列数据结构的示例:https : //github.com/arnauddri/algorithms/blob/master/data- structures%2Fqueue%2Fqueue.go 这是一些代码: 但是,当我尝试创建队列并将项目推送到该队列时,出现运行时错误:

  • 问题内容: 是多线程/进程编程的新手。所以这是我需要澄清的。 处理代码 使用上述伪代码,如果互斥锁未解锁,进程B是否可以访问? 如何从进程B正确访问sharedResource? 有没有清晰的可视化图表说明互斥体,线程和进程之间的关系? 问题答案: 您需要做的是调用pthread_mutex_lock来保护互斥锁,如下所示: 一旦执行此操作,在您在该线程中进行调用之前,不会再进行任何其他调用。因此