国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
thread
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <cstring>
#include <iostream>
using namespace std;
#define THREAD_NUMBER 2
/* 初始化條件變量*/
static pthread_cond_t cond= PTHREAD_COND_INITIALIZER;
/* 初始化mutex*/
static pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;
void* thread1(void *arg)
{
        int ret;
        pthread_mutex_lock(&mutex);
        printf("thread1 locked the mutex\n");
        printf("thread1 is waiting for condition signal...\n");
        cout <<"cond:"<<cond<<endl;
        ret = pthread_cond_wait(&cond, &mutex);
        printf("thread1 received condition signal!\n");
        pthread_mutex_unlock(&mutex);
        sleep(1);
        printf("thread1 unlocked the mutex with ret: %d\n",ret);
        pthread_exit(0);
}
void* thread2(void *arg)
{
        int i = 0,ret;
        struct timeval old, now;
        gettimeofday(&old,NULL);
        now = old;
        pthread_mutex_lock(&mutex);
        printf("thread2 locked the mutex\n");

        /* 休眠5 秒鐘*/
        while (now.tv_sec-old.tv_sec< 5) {
                sleep(1);
                gettimeofday(&now,NULL);
                i++;
                printf("thread2 sleep %d seconds\n", i);
        }
        printf("thread1 calls pthread_cond_signal...\n");
        /* 啟動等待條件變量cond的其他線程*/
        ret = pthread_cond_signal(&cond);
        cout <<"cond:"<<&cond<<endl;
        pthread_mutex_unlock(&mutex);
        printf("thread2 unlocked the mutex with ret: %d\n",ret);
        pthread_exit(0);
}

int main(int argc, char *argv[])
{
        int i;
        int ret_val;
        pthread_mutex_init(&mutex,NULL);
        pthread_t pt[THREAD_NUMBER];
        memset(pt,0,THREAD_NUMBER);
        ret_val = pthread_create(&pt[0], NULL, thread1, NULL);
        if (ret_val != 0 ) {
                printf("pthread_create error!\n");
                exit(1);
        } 
        sleep(1);
        ret_val = pthread_create(&pt[1], NULL, thread2, NULL);
        if (ret_val != 0 )
        {
                printf("pthread_create error!\n");
                exit(1);
        }

        for (i = 0; i < THREAD_NUMBER; i++)
        {
                ret_val = pthread_join(pt[i], NULL);
                if (ret_val != 0) 
                {
                        printf("pthread_join error!\n");
                        exit(1);
                }
        }
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        return 0;
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
多線程條件變量的加深理解
linux多線程下無界緩沖區(qū)的生產(chǎn)者消費者問題
[pthread] 同步機制(3)
200行C代碼實現(xiàn)簡單線程池 - Linux內(nèi)核編程 - 舉世無雙的學(xué)習(xí)之路
Linux多線程編程(不限Linux)
Linux線程池(C語言描述)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服