linux C++ 多線程 pthread_cond_t 使用的簡單例子

 

 

/************************* Condition.h****************************/

#ifndef CONDITION_H_
#define CONDITION_H_

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
using namespace std;

struct UserInfo
{
    char name[50];
    int age;
};

class Condition
{
public:
    Condition();
    virtual ~Condition();

    void start();
private:
    static void *thread1(void *param);
    static void *thread2(void *param);
    static void *thread3(void *param);

    void createThread1();
    void createThread2();
    void createThread3();

    pthread_mutex_t m_lock;
    pthread_cond_t  m_cond;

    UserInfo *userInfo;
};

#endif /* CONDITION_H_ */

 

/********************* Condition.cpp***********************************/

 

#include "Condition.h"
Condition::Condition()
{
    //shareQueue = new queue();
    userInfo = new UserInfo();
    pthread_mutex_init(&m_lock,NULL);
    pthread_cond_init(&m_cond,NULL);
}

Condition::~Condition()
{
    pthread_mutex_destroy(&m_lock);
    pthread_cond_destroy(&m_cond);
}

void Condition::start()
{
    pthread_t t1,t2,t3;
    if(pthread_create(&t1,NULL,thread1,this)!=0)
    {
        printf("create thread1 failed/n");
    }

    if(pthread_create(&t2,NULL,thread2,this)!=0)
    {
        printf("create thread2 failed/n");
    }

    if(pthread_create(&t3,NULL,thread3,this)!=0)
    {
        printf("create thread3 failed/n");
    }
}


void *Condition::thread1(void *param)
{
    pthread_detach(pthread_self());
    Condition *t = (Condition *)param;
    t->createThread1();
    return NULL;
}

void Condition::createThread1()
{
    int count=1;
    while(true)
    {
        pthread_mutex_lock(&m_lock);
        int lock = pthread_cond_wait(&m_cond, &m_lock);
        if (lock == 0)
        {
            sprintf(userInfo->name,"%s","thread1");
            userInfo->age = 1;
            printf(">>>thread1:username=%s,age=%d,count=%d/n", userInfo->name,userInfo->age,count);
            count++;
        }
        else
        {
            printf(">>>thread1 receive signal timeout/n/n");
            break;
        }
        pthread_mutex_unlock(&m_lock);
    }
}

void *Condition::thread2(void *param)
{
    pthread_detach(pthread_self());
    Condition *t = (Condition *)param;
    t->createThread2();
    return NULL;
}

void Condition::createThread2()
{
    int count=1;
    while(true)
    {
        pthread_mutex_lock(&m_lock);
        int lock = pthread_cond_wait(&m_cond, &m_lock);
        if (lock == 0)
        {
            sprintf(userInfo->name,"%s","thread2");
            userInfo->age = 2;
            printf(">>>thread2:username=%s,age=%d,count =%d/n", userInfo->name,userInfo->age,count);
            count++;
        }
        else
        {
            printf(">>>thread2 receive signal timeout/n");
            break;
        }
        pthread_mutex_unlock(&m_lock);
    }
}

void *Condition::thread3(void *param)
{
    pthread_detach(pthread_self());
    Condition *t = (Condition *)param;
    t->createThread3();
    return NULL;
}

void Condition::createThread3()
{
    int count =1;
    for(int i=1;i<100000;i++)
    {
        if(0==(i%1000))
        {
            usleep(4000);
            pthread_mutex_lock(&m_lock);

            //int signal = pthread_cond_signal(&m_cond);
            int signal = pthread_cond_broadcast(&m_cond);
            if(0==signal)
            {
                printf("/n>>>thread3 send signal,count=%d/n",count);
            }
            pthread_mutex_unlock(&m_lock);
            count++;
        }
    }
    return;
}

 

/************************* main.cpp****************************/
#include <stdio.h>
#include "Condition.h"
int main()
{
    Condition t;
    t.start();
    while(true)
    {
        sleep(15);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章