先看例子代码:

void CStudent::menuCallback(CCObject * pSender) {
    CStudent *temp = new CStudent("new student", 33);
    pthread_mutex_init(&mutex, NULL);//create a mutex
    pthread_create(&pidRun, NULL, run, temp); //create a thread
    void *run_thread_status = NULL;
    pthread_join(pidRun, &run_thread_status); // wait the thread dead,then execute the other threads
    pthread_create(&pidGo, NULL, go, 0);

    void *thrid_thread_status = NULL;
    pthread_join(pidGo, &thrid_thread_status);
    pthread_create(&pidThrid, NULL, thrid,NULL);
}

void CStudent::closeCallback(CCObject * pSender) {

}

void *run(void *r) {
    CStudent *pStudent = (CStudent *)r;
    do
    {
        CC_BREAK_IF(pStudent == 0);
        CCLog("Name: %s, age: %d\n", pStudent->getName(), pStudent->getAge());
        while(true) {
            pthread_mutex_lock(&mutex);
            if(ticket > 20) {
                CCLog("Thread Run Sell ticket: %d\n", ticket);
                --ticket;
                pthread_mutex_unlock(&mutex);
            } else {
                pthread_mutex_unlock(&mutex);
                break;
            }
            Sleep(1);
        }
    } while(false);
    return NULL;
}

void *go(void *g) {
    while(true) {
        pthread_mutex_lock(&mutex);
        if(ticket > 10 && ticket <= 20) {
            CCLog("Go Thread Sell ticket: %d\n", ticket);
            --ticket;
            pthread_mutex_unlock(&mutex);// the the mutex unsingled
        } else {
           pthread_mutex_unlock(&mutex);
           break;
        }
    
    }
    Sleep(1);
    return NULL;
}

void *thrid(void *t) {
    while(true) {
        pthread_mutex_lock(&mutex);
        if(ticket > 0 && ticket <= 10) {
            CCLog("Thrid thread Sell ticket: %d\n", ticket);
            --ticket;
            pthread_mutex_unlock(&mutex);
        } else {
            pthread_mutex_unlock(&mutex);
            break;
        }
    }
    return NULL;
}