【问题标题】:std::lock_guard won't unlockstd::lock_guard 不会解锁
【发布时间】:2013-12-17 05:15:20
【问题描述】:

我正在尝试在以下代码中锁定我的互斥锁列表,以便一次只有一个线程可以对其进行搜索、解锁、锁定或修改。

#include <mutex>
#include <map>
#include <memory>
#include <vector>
#include <thread>
#include <atomic>
#include <iostream>
#include <Windows.h>

struct MoveableMutex
{
    std::mutex m;
    MoveableMutex() {}
    MoveableMutex(MoveableMutex const&) {}
    MoveableMutex& operator = (MoveableMutex const&) { return *this; }
};

class Locks
{
    private:
        static std::mutex map_lock;
        static std::uint32_t lock_count;
        std::map<std::uint32_t, MoveableMutex> locklist;

    public:
        std::uint32_t AddLock();
        void RemoveLock(std::uint32_t ID);
        void Lock(std::uint32_t ID);
        bool TryLock(std::uint32_t ID);
        void Unlock(std::uint32_t ID);
};

std::uint32_t Locks::lock_count = 0;
std::mutex Locks::map_lock;

std::uint32_t Locks::AddLock()
{
    std::lock_guard<std::mutex> guard(map_lock);
    locklist.insert(std::make_pair(++lock_count, MoveableMutex()));
    return lock_count;
}

void Locks::RemoveLock(std::uint32_t ID)
{
    std::lock_guard<std::mutex> guard(map_lock);
    auto it = locklist.find(ID);
    if (it != locklist.end())
    {
        it->second.m.unlock();
        locklist.erase(it);
    }
}

void Locks::Lock(std::uint32_t ID)
{
    std::lock_guard<std::mutex> guard(map_lock);
    auto it = this->locklist.find(ID);
    if (it != this->locklist.end())
    {
        it->second.m.lock();
    }
}

bool Locks::TryLock(std::uint32_t ID)
{
    std::lock_guard<std::mutex> guard(map_lock);
    auto it = this->locklist.find(ID);
    if (it != this->locklist.end())
    {
        return it->second.m.try_lock();
    }
    return false;
}

void Locks::Unlock(std::uint32_t ID)
{
    std::lock_guard<std::mutex> guard(map_lock);
    auto it = this->locklist.find(ID);
    if (it != locklist.end())
    {
        it->second.m.unlock();
    }
}

int main()
{
    Locks locklist;
    int i = locklist.AddLock();
    std::atomic<bool> stop(false);
    std::atomic<bool> stop2(false);

    std::thread o([&]
    {
        locklist.Lock(i);
        while(!stop)
        {
            std::cout << "Hey\n";
            Sleep(100);
        }
        locklist.Unlock(i);
    });

    std::thread t([&]
    {
        locklist.Lock(i);
        while(!stop2)
        {
            std::cout << "Hey2\n";
            Sleep(100);
        }
        locklist.Unlock(i);
    });

    Sleep(1000);
    stop = true;
    system("CLS");
    o.join();

    Sleep(1000);
    stop2 = true;
    t.join();
    return 0;
}

但是,在 Unlock 函数中使用 std::lock_guard 会导致死锁。如果我从 Unlock 函数中删除 lock_guard,它就可以正常工作。

lock_guard 没有破坏或解锁是否有原因?

【问题讨论】:

  • 好吧,如果您不信任 std::lock_guard,您可以随时尝试直接使用 map_lock.lock()/map_lock.unlock()。但是,您的问题似乎源于其他地方。

标签: c++ multithreading c++11 mutex


【解决方案1】:

一个线程调用Lock,最终将互斥锁锁定在映射中。另一个线程调用Lock,它锁定map_lock,然后尝试锁定映射中的互斥体,并卡在那里(map_lock 仍然保持)。最终,第一个线程退出循环并调用Unlock,它在map_lock 上等待。

这里的主要设计缺陷是你有一个线程获取两个锁,一个接一个。这只有在所有线程以相同的顺序获取它们(并以获取的相反顺序释放)时才安全工作。但是您的代码在不同的时间以不同的顺序获取它们:这是导致死锁的秘诀。

另请参阅:lock hierarchy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 2013-12-29
    • 2022-11-22
    相关资源
    最近更新 更多