【linux线程】线程安全之条件变量

条件变量用法:

条件变量一般和互斥量配合,保护线程安全或者完成同步数据的功能。

#include <pthread.h>
#define INFINITE 0xFFFFFFFF

#ifndef ETIMEDOUT
#define ETIMEDOUT 10060
#endif

class CWaitObj;

class CWaitObj
{
public:
    explicit CWaitObj(bool manual = false); //explicit 防止一个参数的构造函数进行隐式变化
    
    virtual ~CWaitObj();
    bool    wait(long long msec = INFINITE);
    void    notify();
    void    reset();

protected:
    pthread_mutext_t mutex;
    pthread_cond_t   cond;
    bool signal_flag;
    bool manual_flag;

private:
    CWaitObj(const CWaitObj& src);
    CWaitObj& operator = (const CWaitObj& src);
    void get_abstime_wait(int msec, strut timespec *abstime);
}


CWaitObj::CWaitObj(bool manual)
:manual_flag(manual)
{
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL)
    signal_flag = false;
}

CWaitObj::~CWaitObj()
{
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
}

bool CWait::wait(DWord msec)
{
    pthread_mutex_lock(&mutex);
    int ret = 0;
    if(signal_flag == false)
    {
        if(INFINITE != msec){
            struct timespec nptime;
            get_abstime_wait(msec,&nptime);
            ret = pthread_cond_timedwait(&cond,&mutex,&nptime);
            if(ret && ret != ETIMEDOUT){}
        }
        else{
            ret = pthread_cond_wait(&cond,&mutex);
        }
    }
    if(!manual_flag) signal_flag = false;
    pthread_mutex_unlock(&mutex);

    return (ret == 0)?true:false;
}

void CWaitObj::notify()
{
    pthread_mutex_lock(&mutex);
    signal_flag = true;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
}

void CWaitObj::reset()
{
    pthread_mutex_lock(&mutex);
    signal_flag = false;
    pthread_mutex_unlock(&mutex);
}

void CWaitObj::get_abstime_wait(int msec,struct timespec *abstime)
{
    struct timeval tv;
    long long absmsec;
    gettimeofday(&tv,NULL);
    absmsec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    absmsec += msec;
   
    abstime->tv_sec = absmsec / 1000;
    abstime->tv_nsec = absmsec % 1000 * 1000000;
}

//定义线程函数run
static void* run(void *arg)
{
    CWaitObj* obj = (CWaitObj*)arg;
    while(1)
    {
        cout<<"run start"<<endl;
        obj->wait(2000);//如果不传参数需要等待notify,否则是等待ms
        cout<<"run end"<<endl;
    }
}

int main()
{
    pthread_t tidp;
    CWaitObj* obj = new CWaitObj();
    cout<<"start to create thread"<<endl;
    if(pthread_create(&tidp,NULL,run,(void*)obj) == -1)
    {
        cout<<"create thread error\r\n"<<endl;
        return -1;
    }
    
    if(pthread_join(tidp,NULL)){
        cout<<"thread is not exit...\r\n";
        return -2;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/w1012747007/article/details/83024429
今日推荐