boost::mutex::~mutex(): 断言 `!pthread_mutex_destroy(&m)'失败的

发布于 2024-12-10 19:45:16 字数 998 浏览 0 评论 0原文

我在互斥体析构函数中收到标题错误。由于错误可能是由于互斥体在销毁过程中处于锁定状态所致,因此我创建了一个继承自 boost:mutex 的新互斥体类。这是为了确保互斥锁在销毁期间解锁。然而,同样的错误仍然发生。 任何点击将不胜感激!

class CMutes : public boost::mutex
{
public:
    CMutes()
    {

    };

    virtual ~CMutes()
    {
        if (m_bLock)
            boost::mutex::unlock();
    };

    void lock()
    {
        if(!m_bLock)
            boost::mutex::lock();
        else
            cout << "Mutex is in lock state\n";
    };

    void unlock()
    {
        if (m_bLock)
            boost::mutex::unlock();
        else
            cout << "Mutex is in unlock state\n";
    }

    boost::mutex& getMutex()
    {
        return *this;
    }

private:

    bool m_bLock;
};

编辑: 是的你是对的。我应该使用 RAII。然而,我现在的情况是这样的。我需要在另一个线程完成处理资源之前锁定该资源。像下面这样的东西。

Thread A:
void getDate()
{
 m_oLock.lock();
 // access resource
}

void unlock()
{
 m_oLock.unlock();
}
Thread B:
void Process()
{
 threadA.getData();
 threadA.unlock();
}

I got the captioned error in the mutex destructor. since the error may due to the mutex is in lock state during destruction, I create a new mutex class which is inherited from boost:mutex. it is to make sure the mutex is unlock during destruction. However, the same error still occurs.
Any hits would be appreciated!

class CMutes : public boost::mutex
{
public:
    CMutes()
    {

    };

    virtual ~CMutes()
    {
        if (m_bLock)
            boost::mutex::unlock();
    };

    void lock()
    {
        if(!m_bLock)
            boost::mutex::lock();
        else
            cout << "Mutex is in lock state\n";
    };

    void unlock()
    {
        if (m_bLock)
            boost::mutex::unlock();
        else
            cout << "Mutex is in unlock state\n";
    }

    boost::mutex& getMutex()
    {
        return *this;
    }

private:

    bool m_bLock;
};

EDIT:
Yes you are right. I should use RAII. However, I am in a situation. I need to lock a resource before another thread is finish processing it. something like below.

Thread A:
void getDate()
{
 m_oLock.lock();
 // access resource
}

void unlock()
{
 m_oLock.unlock();
}
Thread B:
void Process()
{
 threadA.getData();
 threadA.unlock();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很坚强 2024-12-17 19:45:16

不要继承自boost::mutexboost::mutex类没有虚拟析构函数,因此它实际上并不意味着遗产。

可能的根本原因:
您收到的错误表明您正在对从未锁定的互斥体调用unlock。类似于:

boost::mutex m;   
m.unlock(); 

通过尝试进行锁定和解锁,您似乎无法跟踪互斥锁是否已锁定。当您手动执行资源管理时,这通常是问题所在。 C++ 允许一种名为资源分配是初始化( RAII),以安全防范此类问题。

建议的解决方案:
您应该使用RAII,而不是显式解锁互斥锁。您可以使用 boost::mutex::scoped_lock 来实现 RAII:

struct YourStruct
{
    void doSomething()
    {
        boost::mutex::scoped_lock l(m_mutex);
        //do something Interesting
    }
    private: 
        boost::mutex m_mutex;
};

Do Not inherit from boost::mutex, the boost::mutex class does not have a virtual destructor, so it is not actually meant for Inheritance.

Possible Root Cause:
The error you are getting indicates that you are calling unlock on a mutex that was never locked. Something like:

boost::mutex m;   
m.unlock(); 

By trying to do lock and unlock, it seems you lose the track of whether the mutex as locked.This is very often the problem when you perform resource management manually. C++ allows a specific mechansim called Resource Allocation is Initilization(RAII) for safe guarding against such problems.

Suggestted Solution:
You should use RAII, rather than unlocking the mutex explicitly. You could use the boost::mutex::scoped_lock to implement RAII:

struct YourStruct
{
    void doSomething()
    {
        boost::mutex::scoped_lock l(m_mutex);
        //do something Interesting
    }
    private: 
        boost::mutex m_mutex;
};
你是我的挚爱i 2024-12-17 19:45:16

我有同样的错误(这就是我发现这个问题的方式)。我通过向相关线程添加 join 解决了问题。我的主进程在线程完成之前就完成了,并且互斥体在解锁之前就被拆除了。

I had the same error (which is how I found this question). I solved by problem by adding a join to the relevant thread. My main process was finishing up before the thread did and the mutex was being torn down before it could be unlocked.

晚雾 2024-12-17 19:45:16

POSIX 规定,如果互斥体因某种原因无效,则从 pthread_mutex_destroy 操作返回的唯一错误是 EINVAL,或者如果有人正在使用它(明确地),则返回 EBUSY或通过条件变量)。

最有可能的情况是第二种。

但是,我没有看到任何代码中的 m_bLock 成员变量发生任何更改。您确定不想在 lockunlock 调用中更改此变量吗?

如果它正在使用,您只需等待,直到使用它的人愿意释放它。任何其他选择都不可能对您有好处:-)

POSIX states that the only errors returned from a pthread_mutex_destroy operation are EINVAL if the mutex is somehow invalid, or EBUSY if someone is using it (explicitly or via condition variables).

The most likely scenario is the second one.

However, I'm not seeing any changes to the m_bLock member variable in any of your code. Are you sure you don't want to change this variable in the lock and unlock calls?

If it is in use, you'll just have to wait until whoever is using it is willing to release it. Any other option is unlikely to be good for you :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文