日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(

發(fā)布時(shí)間:2024/9/27 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach( 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1?pthread_create()函數(shù)

創(chuàng)建線程

A:依賴的頭文件

#include<pthread.h>

B:函數(shù)聲明

int pthread_create(pthread_t *thread, constpthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

pthread_t *thread:傳遞一個(gè)pthread_t變量地址進(jìn)來,用于保存新線程的tid(線程ID

const pthread_attr_t *attr:線程屬性設(shè)置,如使用默認(rèn)屬性,則傳NULL

void *(*start_routine) (void *):函數(shù)指針,指向新線程應(yīng)該加載執(zhí)行的函數(shù)模塊

void *arg:指定線程將要加載調(diào)用的那個(gè)函數(shù)的參數(shù)

?

返回值:成功返回0,失敗返回錯誤號。以前學(xué)過的系統(tǒng)函數(shù)都是成功返回0,失敗返回-1,而錯誤號保存在全局變

errno中,而pthread庫的函數(shù)都是通過返回值返回錯誤號,雖然每個(gè)線程也都有一個(gè)errno,但這是為了兼容其

它函數(shù)接口而提供的,pthread庫本身并不使用它,通過返回值返回錯誤碼更加清晰。

?

gcc編譯鏈接的時(shí)候,后面要加上-lpthread

?

關(guān)于pthread_t

typedef unsigned long int pthread_t;(32位平臺下)

?

在一個(gè)線程中調(diào)用pthread_create()創(chuàng)建新的線程后,當(dāng)前線程從pthread_create()

返回繼續(xù)往下執(zhí)行,而新的線程所執(zhí)行的代碼由我們傳給pthread_create的函數(shù)指針

start_routine決定。start_routine函數(shù)接收一個(gè)參數(shù),是通過pthread_createarg

數(shù)傳遞給它的,該參數(shù)的類型為void *,這個(gè)指針按什么類型解釋由調(diào)用者自己定

義。start_routine的返回值類型也是void *,這個(gè)指針的含義同樣由調(diào)用者自己定義。start_routine返回時(shí),這個(gè)線程就退出了,其它線程可以調(diào)用pthread_join得到start_routine的返回值,類似于父進(jìn)程調(diào)用wait(2)得到子進(jìn)程的退出狀態(tài),稍后詳細(xì)介紹pthread_join

pthread_create成功返回后,新創(chuàng)建的線程的id被填寫到thread參數(shù)所指向的內(nèi)存單

元。我們知道進(jìn)程id的類型是pid_t,每個(gè)進(jìn)程的id在整個(gè)系統(tǒng)中是唯一的,調(diào)用getpid(2)

可以獲得當(dāng)前進(jìn)程的id,是一個(gè)正整數(shù)值。線程id的類型是thread_t,它只在當(dāng)前進(jìn)程中

保證是唯一的,在不同的系統(tǒng)中thread_t這個(gè)類型有不同的實(shí)現(xiàn),它可能是一個(gè)整數(shù)值,

也可能是一個(gè)結(jié)構(gòu)體,也可能是一個(gè)地址,所以不能簡單地當(dāng)成整數(shù)用printf打印,調(diào)用

pthread_self(3)可以獲得當(dāng)前線程的id

attr參數(shù)表示線程屬性,本節(jié)不深入討論線程屬性,所有代碼例子都傳NULLattr

數(shù),表示線程屬性取缺省值,感興趣的讀者可以參考[APUE2e]

?

C案例說明(程序編寫,測試最大能夠創(chuàng)建的線程數(shù))

執(zhí)行下面命令:

運(yùn)行./app,結(jié)果如下:

2?pthread_self()函數(shù)

獲取調(diào)用線程tid

A依賴的頭文件???????????????????????????????????????????????????????????????????????????????????

#include<pthread.h>

B函數(shù)聲明

pthread_t pthread_self(void);

C函數(shù)說明

This function always?succeeds,?returning?the?calling thread's ID.

D案例說明1

運(yùn)行結(jié)果:

說明:

由于pthread_create的錯誤碼不保存在errno中,因此不能直接用perror(3)打印錯誤信

息,可以先用strerror(3)把錯誤碼轉(zhuǎn)換成錯誤信息再打印。

?

E案例說明2

運(yùn)行結(jié)果:

?

3 pthread_exit()函數(shù)

說明:

如果任意一個(gè)線程調(diào)用了exit_exit,則整個(gè)進(jìn)程的所有線程都終止,由于從main函數(shù)return也相當(dāng)于調(diào)用exit,為了防止新創(chuàng)建的線程還沒有得到執(zhí)行就終止,我們在main函數(shù)return之前延遲1秒,這只是權(quán)宜之計(jì),即使主線程等待1秒,內(nèi)核也不一定會調(diào)度新創(chuàng)建的線程執(zhí)行。所以這時(shí)候要用到pthread_exit()

?

pthread_exit()函數(shù)的特點(diǎn)如下:

A:調(diào)用線程退出函數(shù),注意和exit函數(shù)的區(qū)別,任何線程里exit導(dǎo)致進(jìn)程退出,其他線程

未工作結(jié)束,主控線程退出時(shí)不能returnexit

B:需要注意,pthread_exit或者return返回的指針?biāo)赶虻膬?nèi)存單元必須是全局的或者是用malloc分配的,不能在線程函數(shù)的棧上分配,因?yàn)楫?dāng)其它線程得到這個(gè)返回指針時(shí)線程函數(shù)已經(jīng)退出了。

?

?

C依賴的頭文件

#include<pthread.h>

D函數(shù)聲明

voidpthread_exit(void*retval);

E功能:使用函數(shù)pthread_exit退出線程,這是線程的主動行為;由于一個(gè)進(jìn)程中的多個(gè)線程是共享數(shù)據(jù)段的,因此通常在線程退出之后,退出線程所占用的資源并不會隨著線程的終止而得到釋放,但是可以用pthread_join()函數(shù)來同步并釋放資源。

F說明

retval:pthread_exit()調(diào)用線程的返回值,可由其他函數(shù)如pthread_join來檢索獲取。

G案例說明:

運(yùn)行結(jié)果:

此外:pthread_exit()函數(shù)括號中的值是用于傳出的,類似exit(0)exit(1)。這個(gè)值供pthread_join獲取,用以判斷是正常退出還是異常退出。

?

4 pthread_join()函數(shù)

A依賴的頭文件

#include<pthread.h>

B函數(shù)聲明

int pthread_join(pthread_t thread,void**retval);

?

pthread_t thread:回收線程的tid

void **retval:接收退出線程傳遞出的返回值

?

返回值:成功返回0,失敗返回錯誤號

?

調(diào)用該函數(shù)的線程將掛起等待,直到idthread的線程終止。thread線程以不同的方式終止,通過pthread_join得到的終止?fàn)顟B(tài)是不同的,總結(jié)如下:

  • 如果thread線程通過return返回,value_ptr所指向的單元里存放的是thread線程函數(shù)的返回值。

  • 如果thread線程被別的線程調(diào)用pthread_cancel異常終止掉,value_ptr所指向的單元存放的是常熟PTHREAD_CANCELED.

  • 如果thread線程是自己調(diào)用pthread_exit終止的,value_ptr所指向的單元存放的是傳給pthread_exit的參數(shù)。

  • 如果對thread線程的終止?fàn)顟B(tài)不感興趣,可以傳NULLvalue_ptr參數(shù)。

  • ?

    C:函數(shù)描述:

    The pthread_join() function waits for thethread specified by thread to

    ??????terminate.?If that thread hasalready terminated, then?pthread_join()

    ??????returns immediately.?The threadspecified by thread must be joinable.

    ?

    ??????If?retval?is?notNULL, then pthread_join() copies the exit status of

    ??????the target thread (i.e., the value that the target thread?supplied?to

    ??????pthread_exit(3))?into thelocation pointed to by *retval.?If thetar

    ??????get thread was canceled, then PTHREAD_CANCELED is placed in *retval.

    ?

    ??????If multiple threads simultaneously try to join with?the?same?thread,

    ??????the?results?are?undefined.??If the thread callingpthread_join() is

    ??????canceled, then the target thread will remain joinable?(i.e.,?it?will

    ??????not be detached).

    ?

    5 pthread_cancel

    在進(jìn)程內(nèi)某個(gè)線程可以取消另外一個(gè)線程

    A依賴的頭文件

    #include<pthread.h>

    B函數(shù)聲明

    int pthread_cancel(pthread_t thread);

    C函數(shù)說明:

    被取消的線程,退出值,定義在Linuxpthread庫中常數(shù)PTHREAD_CANCELED的值是-1.可以在頭文件pthread.h中找到它的定義:

    #define PTHREAD_CANCELED ((void *) -1)

    D案例說明:

    運(yùn)行結(jié)果:

    ?

    6 pthread_detach()函數(shù)

    A依賴的頭文件

    #include<pthread.h>

    B函數(shù)聲明

    int pthread_detach(pthread_t tid);

    pthread_t tid:分離線程tid

    返回值:成功返回0,失敗返回錯誤號

    C函數(shù)說明:

    ???一般情況下,線程終止后,其終止?fàn)顟B(tài)一直保留到其它線程調(diào)用pthread_join獲取它的

    狀態(tài)為止。但是線程也可以被置為detach狀態(tài),這樣的線程一旦終止就立刻回收它占用的所有資源,而不保留終止?fàn)顟B(tài)。不能對一個(gè)已經(jīng)處于detach狀態(tài)的線程調(diào)用pthread_join,這樣的調(diào)用將返回EINVAL。對一個(gè)尚未detach的線程調(diào)用pthread_joinpthread_detach都可以把該線程置為detach狀態(tài),也就是說,不能對同一線程調(diào)用兩次pthread_join,或者如果已經(jīng)對一個(gè)線程調(diào)用了pthread_detach就不能再調(diào)用pthread_join了。

    E案例說明

    運(yùn)行結(jié)果:

    當(dāng)把代碼不部分的紅框中的代碼取消注釋,在運(yùn)行時(shí),運(yùn)行結(jié)果如下:

    說明:joindetach不能同時(shí)使用

    ?

    7 pthread_equal()函數(shù)

    比較兩個(gè)線程是否相等

    A依賴的頭文件

    #include<pthread.h>

    B函數(shù)聲明

    int pthread_equal(pthread_t t1,pthread_tt2);

    C案例說明

    運(yùn)行結(jié)果:

    總結(jié)

    以上是生活随笔為你收集整理的2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。