boost::mutex::~mutex(): 断言 `!pthread_mutex_destroy(&m)'失败的
我在互斥体析构函数中收到标题错误。由于错误可能是由于互斥体在销毁过程中处于锁定状态所致,因此我创建了一个继承自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要继承自
boost::mutex
,boost::mutex
类没有虚拟析构函数,因此它实际上并不意味着遗产。可能的根本原因:
您收到的错误表明您正在对从未锁定的互斥体调用
unlock
。类似于:通过尝试进行锁定和解锁,您似乎无法跟踪互斥锁是否已锁定。当您手动执行资源管理时,这通常是问题所在。 C++ 允许一种名为资源分配是初始化( RAII),以安全防范此类问题。
建议的解决方案:
您应该使用RAII,而不是显式解锁互斥锁。您可以使用 boost::mutex::scoped_lock 来实现 RAII:
Do Not inherit from
boost::mutex
, theboost::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:By trying to do
lock
andunlock
, 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:
我有同样的错误(这就是我发现这个问题的方式)。我通过向相关线程添加
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 themutex
was being torn down before it could be unlocked.POSIX 规定,如果互斥体因某种原因无效,则从
pthread_mutex_destroy
操作返回的唯一错误是EINVAL
,或者如果有人正在使用它(明确地),则返回EBUSY
或通过条件变量)。最有可能的情况是第二种。
但是,我没有看到任何代码中的
m_bLock
成员变量发生任何更改。您确定不想在lock
和unlock
调用中更改此变量吗?如果它正在使用,您只需等待,直到使用它的人愿意释放它。任何其他选择都不可能对您有好处:-)
POSIX states that the only errors returned from a
pthread_mutex_destroy
operation areEINVAL
if the mutex is somehow invalid, orEBUSY
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 thelock
andunlock
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 :-)