pthread_mutex_lock错误
#include "MutexCondition.h"
bool MutexCondition::init(){
printf("MutexCondition::init called\n");
pthread_mutex_init(&m_mut, NULL);
pthread_cond_init(&m_con, NULL);
return true;
}
bool MutexCondition::destroy(){
pthread_mutex_destroy(&m_mut);
pthread_cond_destroy(&m_con);
return true;
}
bool MutexCondition::lock(){
pthread_mutex_lock(&m_mut);
return true;
}
bool MutexCondition::unLock(){
pthread_mutex_unlock(&m_mut);
return true;
}
bool MutexCondition::wait(){
pthread_cond_wait(&m_con, &m_mut);
return true;
}
bool MutexCondition::signal(){
pthread_cond_signal(&m_con);
return true;
}
我正在从事网络编程,
#ifndef SOUND_H_
#define SOUND_H_
#include <list>
#include "SoundMetaData.h"
#include "SoundSignature.h"
#include "../MutexCondition.h"
using namespace std;
class Sound : public MutexCondition{
private:
SoundMetaData *m_metaData;
list<SoundSignature*> m_soundSignatureList;
public:
Sound(SoundMetaData *metaData);
virtual ~Sound();
SoundMetaData* getSoundMetaData();
list<SoundSignature*> getSoundSignatureList();
bool addApplication(string &application);
bool removeApplication(string &application);
void addSoundSignature(SoundSignature* signature);
};
#endif /* SOUND_H_ */
如果我在 gdb 上运行我的服务器,我有这个和 Sound 类,它扩展了 MutexCondition。它在函数 getSoundSignatureList 的 pthread_mutex_lock () 上爆炸。
list<SoundSignature *> Sound::getSoundSignatureList(){
lock();
list<SoundSignature*> list(m_soundSignatureList);
unLock();
return list;
}
我有一个名为 Engine 的类,5 个不同的线程根据收到的数据包类型创建 Engine 类。 Engine 类中的函数调用 getSoundSignatureList 类。还有任何其他地方可以调用 Engine 类。
我不明白它如何在 pthred_mutex_lock 上爆炸
我该如何解决这个问题?感谢您的帮助
编辑 .h 文件
#ifndef MUTEXCONDITION_H_
#define MUTEXCONDITION_H_
#include <pthread.h>
#include <stdio.h>
class MutexCondition {
private:
bool init();
bool destroy();
protected:
pthread_mutex_t m_mut;
pthread_cond_t m_con;
public:
MutexCondition(){
init();
}
virtual ~MutexCondition(){
destroy();
}
bool lock();
bool unLock();
bool wait();
bool signal();
};
#endif /* MUTEXCONDITION_H_ */
#include "MutexCondition.h"
bool MutexCondition::init(){
printf("MutexCondition::init called\n");
pthread_mutex_init(&m_mut, NULL);
pthread_cond_init(&m_con, NULL);
return true;
}
bool MutexCondition::destroy(){
pthread_mutex_destroy(&m_mut);
pthread_cond_destroy(&m_con);
return true;
}
bool MutexCondition::lock(){
pthread_mutex_lock(&m_mut);
return true;
}
bool MutexCondition::unLock(){
pthread_mutex_unlock(&m_mut);
return true;
}
bool MutexCondition::wait(){
pthread_cond_wait(&m_con, &m_mut);
return true;
}
bool MutexCondition::signal(){
pthread_cond_signal(&m_con);
return true;
}
I am working on a networking programming and I have this and Sound class which extends MutexCondition
#ifndef SOUND_H_
#define SOUND_H_
#include <list>
#include "SoundMetaData.h"
#include "SoundSignature.h"
#include "../MutexCondition.h"
using namespace std;
class Sound : public MutexCondition{
private:
SoundMetaData *m_metaData;
list<SoundSignature*> m_soundSignatureList;
public:
Sound(SoundMetaData *metaData);
virtual ~Sound();
SoundMetaData* getSoundMetaData();
list<SoundSignature*> getSoundSignatureList();
bool addApplication(string &application);
bool removeApplication(string &application);
void addSoundSignature(SoundSignature* signature);
};
#endif /* SOUND_H_ */
If I run my server on gdb. It blows up on pthread_mutex_lock () from the function getSoundSignatureList.
list<SoundSignature *> Sound::getSoundSignatureList(){
lock();
list<SoundSignature*> list(m_soundSignatureList);
unLock();
return list;
}
I have a class called Engine and 5 different threads create the Engine class based on the packet type it received. A function in the Engine class calls the getSoundSignatureList class. There is any other place where call the Engine class.
I don't understand how it could blow up on the pthred_mutex_lock
How do I fix this problems? Thanks for your help
EDIT .h file
#ifndef MUTEXCONDITION_H_
#define MUTEXCONDITION_H_
#include <pthread.h>
#include <stdio.h>
class MutexCondition {
private:
bool init();
bool destroy();
protected:
pthread_mutex_t m_mut;
pthread_cond_t m_con;
public:
MutexCondition(){
init();
}
virtual ~MutexCondition(){
destroy();
}
bool lock();
bool unLock();
bool wait();
bool signal();
};
#endif /* MUTEXCONDITION_H_ */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想知道您的互斥体实例是否由于复制或赋值操作而被丢弃(您甚至可能没有意识到)。互斥体是不可复制的 - 您应该确保您所拥有的包装类不能通过将复制向量和操作符 =() 设为私有且未实现(或类似技术)来复制:
另一个注意事项:在我看来,此类的私有继承可能比公共继承更合适。
I wonder if your mutex instance is being trashed due to a copy or assignment operation (that you may not even be aware of). Mutexes aren't copyable - you should make sure the wrapper classes you have for the can't be copied by making the copy-ctor and operator=() private and unimplemented (or similar technique):
Another note: it seems to me that private inheritance of this class might be more appropriate than public inheritance.