關(guān)于C語言多線程pthread庫的相關(guān)函數(shù)說明
線程相關(guān)操作說明
一 pthread_t
pthread_t在頭文件/usr/include/bits/pthreadtypes.h中定義:
typedef unsigned long int pthread_t;
它是一個線程的標(biāo)識符。
二 pthread_create
函數(shù)pthread_create用來創(chuàng)建一個線程,它的原型為:
extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,
void *(*__start_routine) (void *), void *__arg));
第一個參數(shù)為指向線程標(biāo)識符的指針,第二個參數(shù)用來設(shè)置線程屬性,第三個參數(shù)是線程運(yùn)行函數(shù)的起始地址,最后一個參數(shù)是運(yùn)行函數(shù)的參數(shù)。這里,我們的函數(shù)thread不需要參數(shù),所以最后一個參數(shù)設(shè)為空指針。第二個參數(shù)我們也設(shè)為空指針,這樣將生成默認(rèn)屬性的線程。對線程屬性的設(shè)定和修改我們將在下一節(jié)闡述。當(dāng)創(chuàng)建線程成功時,函數(shù)返回0,若不為0則說明創(chuàng)建線程失敗,常見的錯誤返回代碼為EAGAIN和EINVAL.前者表示系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過多了;后者表示第二個參數(shù)代表的線程屬性值非法。創(chuàng)建線程成功后,新創(chuàng)建的線程則運(yùn)行參數(shù)三和參數(shù)四確定的函數(shù),原來的線程則繼續(xù)運(yùn)行下一行代碼。
三 pthread_join pthread_exit
函數(shù)pthread_join用來等待一個線程的結(jié)束。函數(shù)原型為:
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一個參數(shù)為被等待的線程標(biāo)識符,第二個參數(shù)為一個用戶定義的指針,它可以用來存儲被等待線程的返回值。這個函數(shù)是一個線程阻塞的函數(shù),調(diào)用它的函數(shù)將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時,被等待線程的資源被收回。一個線程的結(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中的第二個參數(shù)thread_return不是NULL,這個值將被傳遞給 thread_return.最后要說明的是,一個線程不能被多個線程等待,否則第一個接收到信號的線程成功返回,其余調(diào)用pthread_join的線程則返回錯誤代碼ESRCH.
在這一節(jié)里,我們編寫了一個最簡單的線程,并掌握了最常用的三個函數(shù)pthread_create,pthread_join和pthread_exit.下面,我們來了解線程的一些常用屬性以及如何設(shè)置這些屬性。
互斥鎖相關(guān)
互斥鎖用來保證一段時間內(nèi)只有一個線程在執(zhí)行一段代碼。
一 pthread_mutex_init
函數(shù)pthread_mutex_init用來生成一個互斥鎖。NULL參數(shù)表明使用默認(rèn)屬性。如果需要聲明特定屬性的互斥鎖,須調(diào)用函數(shù) pthread_mutexattr_init.函數(shù)pthread_mutexattr_setpshared和函數(shù) pthread_mutexattr_settype用來設(shè)置互斥鎖屬性。前一個函數(shù)設(shè)置屬性pshared,它有兩個取值, PTHREAD_PROCESS_PRIVATE和PTHREAD_PROCESS_SHARED.前者用來不同進(jìn)程中的線程同步,后者用于同步本進(jìn)程的不同線程。在上面的例子中,我們使用的是默認(rèn)屬性PTHREAD_PROCESS_ PRIVATE.后者用來設(shè)置互斥鎖類型,可選的類型有PTHREAD_MUTEX_NORMAL、PTHREAD_MUTEX_ERRORCHECK、 PTHREAD_MUTEX_RECURSIVE和PTHREAD _MUTEX_DEFAULT.它們分別定義了不同的上所、解鎖機(jī)制,一般情況下,選用最后一個默認(rèn)屬性。
二 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np
pthread_mutex_lock聲明開始用互斥鎖上鎖,此后的代碼直至調(diào)用pthread_mutex_unlock為止,均被上鎖,即同一時間只能被一個線程調(diào)用執(zhí)行。當(dāng)一個線程執(zhí)行到pthread_mutex_lock處時,如果該鎖此時被另一個線程使用,那此線程被阻塞,即程序?qū)⒌却搅硪粋€線程釋放此互斥鎖。
下面先來一個實(shí)例。我們通過創(chuàng)建兩個線程來實(shí)現(xiàn)對一個數(shù)的遞加。
#include #include #include #include #define MAX 10 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 < MAX; i++) { printf("thread1 : number = %d\n",number); pthread_mutex_lock(&mut); number++; pthread_mutex_unlock(&mut); sleep(2); } printf("thread1 :主函數(shù)在等我完成任務(wù)嗎?\n"); pthread_exit(NULL); } void *thread2() { printf("thread2 : I'm thread 2\n"); for (i = 0; i < MAX; i++) { printf("thread2 : number = %d\n",number);[nextpage] pthread_mutex_lock(&mut); number++; pthread_mutex_unlock(&mut); sleep(3); } printf("thread2 :主函數(shù)在等我完成任務(wù)嗎?\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("線程1被創(chuàng)建\n"); if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3 printf("線程2創(chuàng)建失敗"); else printf("線程2被創(chuàng)建\n"); } void thread_wait(void) { /*等待線程結(jié)束*/ if(thread[0] !=0) { //comment4 pthread_join(thread[0],NULL); printf("線程1已經(jīng)結(jié)束\n"); } if(thread[1] !=0) { //comment5 pthread_join(thread[1],NULL); printf("線程2已經(jīng)結(jié)束\n"); } } int main() { /*用默認(rèn)屬性初始化互斥鎖*/ pthread_mutex_init(&mut,NULL); printf("我是主函數(shù)哦,我正在創(chuàng)建線程,呵呵\n"); thread_create(); printf("我是主函數(shù)哦,我正在等待線程完成任務(wù)阿,呵呵\n"); thread_wait(); return 0; }
下面我們先來編譯、執(zhí)行一下
引文:
falcon@falcon:~/program/c/code/ftp$ gcc -lpthread -o thread_example thread_example.c
falcon@falcon:~/program/c/code/ftp$ ./thread_example
我是主函數(shù)哦,我正在創(chuàng)建線程,呵呵
線程1被創(chuàng)建
線程2被創(chuàng)建
我是主函數(shù)哦,我正在等待線程完成任務(wù)阿,呵呵
thread1 : I'm thread 1
thread1 : number = 0
thread2 : I'm thread 2
thread2 : number = 1
thread1 : number = 2
thread2 : number = 3
thread1 : number = 4
thread2 : number = 5
thread1 : number = 6
thread1 : number = 7
thread2 : number = 8
thread1 : number = 9
thread2 : number = 10
thread1 :主函數(shù)在等我完成任務(wù)嗎?
線程1已經(jīng)結(jié)束
thread2 :主函數(shù)在等我完成任務(wù)嗎?
線程2已經(jīng)結(jié)束
以上這篇關(guān)于C語言多線程pthread庫的相關(guān)函數(shù)說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
上一篇:c語言 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)之字符串
欄 目:C語言
下一篇:C語言實(shí)現(xiàn)用戶態(tài)線程庫案例
本文標(biāo)題:關(guān)于C語言多線程pthread庫的相關(guān)函數(shù)說明
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1575.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法