开发者

pthread_mutex_lock error

开发者 https://www.devze.com 2023-03-26 09:24 出处:网络
#include \"MutexCondition.h\" bool MutexCondition::init(){ printf(\"MutexCondition::init called\\n\"); pthread_mutex_init(&m_mut, NULL);
#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::unLoc开发者_JS百科k(){
    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_ */


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号