pthread_mutex_lock错误

发布于 2024-11-27 22:25:20 字数 2401 浏览 1 评论 0原文

#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 技术交流群。

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

发布评论

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

评论(1

苏璃陌 2024-12-04 22:25:20

我想知道您的互斥体实例是否由于复制或赋值操作而被丢弃(您甚至可能没有意识到)。互斥体是不可复制的 - 您应该确保您所拥有的包装类不能通过将复制向量和操作符 =() 设为私有且未实现(或类似技术)来复制:

#ifndef MUTEXCONDITION_H_
#define MUTEXCONDITION_H_

#include <pthread.h>
#include <stdio.h>

class MutexCondition {

private:
    bool init();
    bool destroy();

    // idiom to prevent copying: don't implement these
    MutexCondition( MutexCondition const&);
    void operator=( MutexCondition const&);

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_ */

另一个注意事项:在我看来,此类的私有继承可能比公共继承更合适。

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):

#ifndef MUTEXCONDITION_H_
#define MUTEXCONDITION_H_

#include <pthread.h>
#include <stdio.h>

class MutexCondition {

private:
    bool init();
    bool destroy();

    // idiom to prevent copying: don't implement these
    MutexCondition( MutexCondition const&);
    void operator=( MutexCondition const&);

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_ */

Another note: it seems to me that private inheritance of this class might be more appropriate than public inheritance.

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