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

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
多線程編程實(shí)例---pthread_join函數(shù)詳解
http://wenku.baidu.com/view/0adbfd86bceb19e8b8f6ba0b.html
單處理器上的
linux多線程,是通過分時(shí)操作完成的;

此時(shí)互斥鎖的作用,只有在時(shí)間足夠的情況下才能體現(xiàn)出來,即有時(shí)線程內(nèi)需要延時(shí);

否則只有第一個(gè)線程不斷解鎖和獲鎖,別的線程在第一個(gè)線程執(zhí)行完前無法獲得互斥鎖。

pthread_join pthread_exit

  

函數(shù)pthread_join用來等待一個(gè)線程的結(jié)束。函數(shù)原型為:

  extern int pthread_join __P ((pthread_t __th, void **__thread_return));

   第一個(gè)參數(shù)為被等待的線程標(biāo)識(shí)符,第二個(gè)參數(shù)為一個(gè)用戶定義的指針,它可以用來存儲(chǔ)被等待線程的返回值。這個(gè)函數(shù)是一個(gè)線程阻塞的函數(shù),調(diào)用它的函數(shù)將 一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時(shí),被等待線程的資源被收回。一個(gè)線程的結(jié)束有兩種途徑,一種是象我們上面的例子一樣,函數(shù)結(jié)束了,調(diào)用它的 線程也就結(jié)束了;另一種方式是通過函數(shù)pthread_exit來實(shí)現(xiàn)。它的函數(shù)原型為:

  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));

   唯一的參數(shù)是函數(shù)的返回代碼,只要pthread_join中的第二個(gè)參數(shù)thread_return不是NULL,這個(gè)值將被傳遞給 thread_return。最后要說明的是,一個(gè)線程不能被多個(gè)線程等待,否則第一個(gè)接收到信號(hào)的線程成功返回,其余調(diào)用pthread_join的線 程則返回錯(cuò)誤代碼ESRCH。

  在這一節(jié)里,我們編寫了一個(gè)最簡單的線程,并掌握了最常用的三個(gè)函數(shù)pthread_create,pthread_joinpthread_exit。下面,我們來了解線程的一些常用屬性以及如何設(shè)置這些屬性。

 

///////////////////////////////////////////////////////////////////////////

源程序:

/*thread_example.c :  c multiple thread programming in linux

   */

#include

#include

#include

#include

#define MAX1 10

#define MAX2 30

pthread_t thread[2];

pthread_mutex_t mut;

int number=0, i;

void *thread1()

{

        printf ("thread1 : I'm thread 1\n");

        for (i = 0; i < MAX1; i++)

        {

                printf("thread1 : number = %d  i=%d\n",number,i);

                pthread_mutex_lock(&mut);

                        number++;

                pthread_mutex_unlock(&mut);

                sleep(2);

        }

 

        printf("thread1 :Is main function waiting for me acomplishing task? \n");

        pthread_exit(NULL);

}

void *thread2()

{

        printf("thread2 : I'm thread 2\n");

        for (i = 0; i < MAX2; i++)

        {

                printf("thread2 : number = %d  i=%d\n",number,i);

                pthread_mutex_lock(&mut);

                        number++;

                pthread_mutex_unlock(&mut);

                sleep(3);

        }

 

        printf("thread2 :Is main function waiting for me  to acomplish task ?\n");

        pthread_exit(NULL);

}

void thread_create(void)

{

        int temp;

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

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

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

                printf("線程1創(chuàng)建失敗!\n");

        else

                printf("Thread 1 is established\n");

        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3

                printf("線程2創(chuàng)建失敗");

        else

                printf("Thread 2 is established\n");

}

void thread_wait(void)

{

        /*等待線程結(jié)束*/

        if(thread[0] !=0) {                   //comment4

                pthread_join(thread[0],NULL);

                printf("Thread 1 is over \n");

        }

        if(thread[1] !=0) {                //comment5

                pthread_join(thread[1],NULL);

                printf("Thread 2 is over\n");

        }

}

int main()

{

        /*用默認(rèn)屬性初始化互斥鎖*/

        pthread_mutex_init(&mut,NULL);

        printf("I am the main funtion,and I am establishing threads. Ha-ha\n");

        thread_create();

        printf("I am the main funtion,and I am waiting for thread to accomplish task. Ha-ha\n");

        thread_wait();

        return 0;

}

///////////////////////////////////////////////////////////

執(zhí)行情況1(linux終端)

[root@localhost root]# gcc -o joint joint.c -lpthread

[root@localhost root]# ./joint

I am the main funtion,and I am establishing threads. Ha-ha

thread1 : I'm thread 1

thread1 : number = 0  i=0

Thread 1 is established

thread2 : I'm thread 2

thread2 : number = 1  i=0

Thread 2 is established

I am the main funtion,and I am waiting for thread to accomplish task. Ha-ha

thread1 : number = 2  i=1

thread2 : number = 3  i=2

thread1 : number = 4  i=3

thread2 : number = 5  i=4

thread1 : number = 6  i=5

thread1 : number = 7  i=6

thread2 : number = 8  i=7

thread1 : number = 9  i=8

thread2 : number = 10  i=9

thread1 :Is main function waiting for me acomplishing task?

Thread 1 is over

thread2 : number = 11  i=11

thread2 : number = 12  i=12

thread2 : number = 13  i=13

thread2 : number = 14  i=14

thread2 : number = 15  i=15

thread2 : number = 16  i=16

thread2 : number = 17  i=17

thread2 : number = 18  i=18

thread2 : number = 19  i=19

thread2 : number = 20  i=20

thread2 : number = 21  i=21

thread2 : number = 22  i=22

thread2 : number = 23  i=23

thread2 : number = 24  i=24

thread2 : number = 25  i=25

thread2 : number = 26  i=26

thread2 : number = 27  i=27

thread2 : number = 28  i=28

thread2 : number = 29  i=29

thread2 :Is main function waiting for me  to acomplish task ?

Thread 2 is over

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
linux c多線程編程實(shí)例代碼
linux下C語言多線程編程實(shí)例
pthread
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服