iOS多線程之pthread

什么是pthread

POSIX線程(POSIX threads),簡稱Pthreads,是線程的POSIX標(biāo)準(zhǔn)。該標(biāo)準(zhǔn)定義了創(chuàng)建和操縱線程的一整套API。在類Unix操作系統(tǒng)(Unix、Linux、Mac OS X等)中,都使用Pthreads作為操作系統(tǒng)的線程。

pthread的使用

一般情況在iOS開發(fā)中我們很少使用pthread來創(chuàng)建線程,pthread常見跨平臺框架中多線程創(chuàng)建。pthread是c語言框架,在程序開發(fā)過程中需要程序員自己管理內(nèi)存。

1、使用pthread創(chuàng)建線程

在使用pthread創(chuàng)建線程前需要導(dǎo)入:#import<pthread.h>

- (void)pthreadDemo{

//? ? pthread屬于POSIX多線程開發(fā)框架

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

?? ? *參數(shù)1:<#pthread_t? _Nullable *restrict _Nonnull#> 指向線程代號的指針

?? ? *參數(shù)2:<#const pthread_attr_t *restrict _Nullable#> 線程的屬性

?? ? *參數(shù)3:<#void * _Nullable (* _Nonnull)(void * _Nullable)#> 指向函數(shù)(線程要執(zhí)行的函數(shù))的指針

? ? ? void *? (*)? ? ? (void *)

?? ? 返回值? (函數(shù)指針)? (參數(shù))

?? ? void *? 和OC中的? id 是等價的!

?????*參數(shù)4:<#void *restrict _Nullable#> 傳遞給該函數(shù)的參數(shù)


?? ? 返回值:在c語言框架中常見,0表示正確,非0表示不正確

?? ? */

? ? NSString *str = @"pthreadDemo";

? ? pthread_tthreadId;

? ? intresult =pthread_create(&threadId,NULL, &demo, (__bridgevoid*)str);//混合開發(fā)時,如果在 C 和 OC 之間傳遞數(shù)據(jù),需要使用 __bridge 進(jìn)行橋接,橋接的目的就是為了告訴編譯器如何管理內(nèi)存。

? ? if(result ==noErr) {//?noErr == 0

? ? ? ? NSLog(@"OK");

? ? }else{

? ? ? ? NSLog(@"error %d",result);

? ? }

}

//線程要執(zhí)行的函數(shù)

void*demo(void*para){

? ? NSLog(@"%@%@",[NSThread currentThread],para);

? ? return NULL;

}

2、pthread內(nèi)置函數(shù)

pthread_create()://創(chuàng)建一個線程

pthread_exit()://終止當(dāng)前線程

pthread_cancel()://中斷另外一個線程的運(yùn)行

pthread_join()://阻塞當(dāng)前的線程,直到另外一個線程運(yùn)行結(jié)束

pthread_attr_init()://初始化線程的屬性

pthread_attr_setdetachstate()://設(shè)置脫離狀態(tài)的屬性(決定這個線程在終止時是否可以被結(jié)合)

pthread_attr_getdetachstate()://獲取脫離狀態(tài)的屬性

pthread_attr_destroy()://刪除線程的屬性

pthread_kill()://向線程發(fā)送一個信號

用于 mutex 和條件變量

pthread_mutex_init()???? //初始化互斥鎖

pthread_mutex_destroy() ? ?// 刪除互斥鎖

pthread_mutex_lock(): ? ?//占有互斥鎖(阻塞操作)

pthread_mutex_trylock(): ? ?//試圖占有互斥鎖(不阻塞操作)。即,當(dāng)互斥鎖空閑時,將占有該鎖;否則,立即返回。

pthread_mutex_unlock(): ? ? //釋放互斥鎖

pthread_cond_init(): ? ?//初始化條件變量

pthread_cond_destroy(): ? ?//銷毀條件變量

pthread_cond_signal(): ? ? //喚醒第一個調(diào)用pthread_cond_wait()而進(jìn)入睡眠的線程

pthread_cond_wait(): ? ? //等待條件變量的特殊條件發(fā)生

Thread-local storage(或者以Pthreads術(shù)語,稱作線程特有數(shù)據(jù)):

pthread_key_create():???? //分配用于標(biāo)識進(jìn)程中線程特定數(shù)據(jù)的鍵

pthread_setspecific(): ? ? //為指定線程特定數(shù)據(jù)鍵設(shè)置線程特定綁定

pthread_getspecific(): ? ? ? ? //獲取調(diào)用線程的鍵綁定,并將該綁定存儲在 value 指向的位置中

pthread_key_delete(): ? ? //銷毀現(xiàn)有線程特定數(shù)據(jù)鍵

pthread_attr_getschedparam(); ? ?//獲取線程優(yōu)先級

pthread_attr_setschedparam(); ? ?//設(shè)置線程優(yōu)先級


pthread_equal(): ? ? //對兩個線程的線程標(biāo)識號進(jìn)行比較

pthread_detach(): ? ? //分離線程

pthread_self(): ? ? //查詢線程自身線程標(biāo)識號


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容