一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

自己寫的C語言線程同步的一個測試例子

 astrotycoon 2013-08-17
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
#define MAXTHREAD 100
pthread_t thread[MAXTHREAD];
pthread_mutex_t mut;
int number=0;

typedef struct
{
    int a;
}countStruct;

void *thread1(void *threadcount)
{
    countStruct *tc;
    int i;
    int count;
    tc = (countStruct *)threadcount;
// printf ("thread1 : I'm thread 1\n");

        for (i = 0; i < MAX; i++)
        {
// printf("thread[%d] : number = %d, i = %d\n", *tc, number, i);

        pthread_mutex_lock(&mut);
//        usleep(500000);

        count = number;
        usleep(100);
                count++;
        number = count;
                pthread_mutex_unlock(&mut);
        printf("thread[%d] : number++ = %d, i = %d\n", tc->a, number, pthread_self());
// sleep(2);

        }
// printf("thread1 :主函數(shù)在等我完成任務嗎?\n");

        pthread_exit(NULL);
}

//void thread_create(void)

//{

// int temp, k;

// memset(&thread, 0, sizeof(thread)); //comment1

// /*創(chuàng)建線程*/

//    for(k = 0; k < MAXTHREAD; k++)

//    {

////        sleep(1);

//        if((temp = pthread_create(&thread[k], NULL, thread1, (void *)&k)) != 0) //comment2

//

//            printf("thread create error!\n");

//        else

//            printf("thread[%d] is created\n", k);

//    }

//}

void thread_wait(void)
{
    int i;

    for(i = 0; i < MAXTHREAD; i++)
    {
        if(thread[i] != 0)
        {
            pthread_join(thread[i],NULL);
     printf("thread[%d] is finished\n", i);
        }
    }

}
int main()
{
    int temp,k;
    countStruct cs[MAXTHREAD];

        /*用默認屬性初始化互斥鎖*/
        pthread_mutex_init(&mut,NULL);
        printf("i'm main function, i'm creating threads\n");
    
    ///////////////////

        memset(&thread, 0, sizeof(thread)); //comment1

        /*創(chuàng)建線程*/
    for(k = 0; k < MAXTHREAD; k++)
    {
        cs[k].a = k;
        if((temp = pthread_create(&thread[k], NULL, thread1, (void *)&cs[k])) != 0) //comment2


            printf("thread create error!\n");
        else
            printf("thread[%d] is created\n", k);
    }
    ///////////////////

        
        printf("i'm main function, i'm waiting for my threads\n");
        thread_wait();
        return 0;
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    欧洲偷拍视频中文字幕| 热久久这里只有精品视频| 国产男女激情在线视频| 九九热国产这里只有精品| 国产成人精品一区二区在线看| 免费在线观看欧美喷水黄片| 国产日韩欧美专区一区| 白丝美女被插入视频在线观看| 欧美日韩国产欧美日韩| 人人妻在人人看人人澡| 久草国产精品一区二区| 国产传媒欧美日韩成人精品| 国产精品白丝久久av| 欧美加勒比一区二区三区 | 欧美日韩精品一区二区三区不卡| 国产午夜精品福利免费不| 欧美精品在线观看国产| 国产成人午夜福利片片| 樱井知香黑人一区二区| 亚洲中文字幕一区三区| 91亚洲精品国产一区| 99热在线播放免费观看| 久久这里只精品免费福利| 一区二区三区在线不卡免费| 国产精品视频第一第二区| 少妇激情在线免费观看| 日韩黄色一级片免费收看| 97人妻精品一区二区三区免| 亚洲国产中文字幕在线观看| 国产一区麻豆水好多高潮| 日韩亚洲激情在线观看| 日韩精品中文字幕亚洲| 久久热九九这里只有精品| 国产精品亚洲综合天堂夜夜| 亚洲欧美日韩国产成人| 麻豆果冻传媒一二三区| 邻居人妻人公侵犯人妻视频| 台湾综合熟女一区二区| 日韩精品区欧美在线一区| 中国一区二区三区不卡| 日本亚洲精品在线观看|