gpt4 book ai didi

c++ - 为自己的互斥锁类使用 lock_guard

转载 作者:行者123 更新时间:2023-11-28 02:40:16 30 4
gpt4 key购买 nike

我写了 3 个互斥类 TMutex、TCondition 和 TSpinLock 都有一个 void lock() 和一个 void unlock() 成员。现在我想对它们使用 std::lock_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: error: no matching function for call to 'std::lock_guard::lock_guard(app::TCondition&)' ../src/inc/threads.cpp:317:39: note: candidates are: /usr/include/c++/4.6/mutex:447:7: note: std::lock_guard<_Mutex>::lock_guard(std::lock_guard<_Mutex>::mutex_type&, std::adopt_lock_t) [with _Mutex = std::mutex, std::lock_guard<_Mutex>::mutex_type = std::mutex] /usr/include/c++/4.6/mutex:447:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/4.6/mutex:444:16: note: std::lock_guard<_Mutex>::lock_guard(std::lock_guard<_Mutex>::mutex_type&) [with _Mutex = std::mutex, std::lock_guard<_Mutex>::mutex_type = std::mutex] /usr/include/c++/4.6/mutex:444:16: note: no known conversion for argument 1 from 'app::TCondition' to 'std::lock_guard::mutex_type& {aka std::mutex&}'

我还尝试将我自己的 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: error: no matching function for call to 'app::TLockGuard::TLockGuard(app::TCondition&)' ../src/inc/threads.cpp:318:39: note: candidate is: ../src/inc/semaphore.h:164:12: note: app::TLockGuard::TLockGuard(app::TLockGuard::lock_t&) [with T = std::mutex, app::TLockGuard::lock_t = std::mutex] ../src/inc/semaphore.h:164:12: note: no known conversion for argument 1 from 'app::TCondition' to 'app::TLockGuard::lock_t& {aka std::mutex&}'

有人可以帮助我了解这两种情况出了什么问题吗?我更愿意使用标准的 lock_guard,但如果我自己的包装器也能工作,那就太好了。

最佳答案

以下示例编译良好:

#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 的 lock_guard,但您传递了一个 TCondition。

尝试更改为:

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

关于c++ - 为自己的互斥锁类使用 lock_guard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26221978/

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