Linux下線程編程
1.線程相關(guān)函數(shù)
?1.1創(chuàng)建線程pthread_create
??pthread_create是Unix操作系統(tǒng)(Unix、linux等)的創(chuàng)建線程的函數(shù)。
??注:編譯時需要指定鏈接庫 -lpthread
??函數(shù)原型:
#include
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
形參: thread — 指向線程標(biāo)志符的指針類型為:pthread_t *
??attr — 設(shè)置線程屬性,默認(rèn)填NULL。類型為:const pthread_attr_t *
??void *(*start_routine) (void *) — 函數(shù)指針,現(xiàn)在運(yùn)行函數(shù)的起始地址
??arg — 運(yùn)行函數(shù)的參數(shù)。不需要填NULL ,類型為:void *
返回值: 成功返回0;失敗返回錯誤編號。
??線程創(chuàng)建成功后,attr參數(shù)用于指定線程屬性,新創(chuàng)建的線程函數(shù)形參只有一個void *形參,若需要傳入的參數(shù)不止一個,則可以把需要傳入的參數(shù)保存到一個結(jié)構(gòu)體中,通過結(jié)構(gòu)體傳入。
??示例:
#include
#include
#include
void *start_routine_func(void *arg)
{
while (1)
{
printf("子線程運(yùn)行中。。。\n");
sleep(1);
}
}
int main()
{
int stat;
pthread_t pth;//線程標(biāo)志符
pthread_create(&pth,NULL,start_routine_func,NULL);
while(1)
{
printf("主線程運(yùn)行中。。。\n");
sleep(1);
}
}
[xsw@xsw 系統(tǒng)編程]$ gcc pthread.c -l pthread
[xsw@xsw 系統(tǒng)編程]$ ./a.out
主線程運(yùn)行中。。。
子線程運(yùn)行中。。。
主線程運(yùn)行中。。。
子線程運(yùn)行中。。。
子線程運(yùn)行中。。。
主線程運(yùn)行中。。。
?1.2 退出線程pthread_exit
??函數(shù)原型:
void pthread_exit(void *retval);
函數(shù)功能:
??終止調(diào)用它的線程并通過形參返回一個指向某個對象的指針
形 參: void *retval — 線程需要返回的地址
返回值: 無
??注:線程結(jié)束必須釋放線程堆棧,也就是線程函數(shù)必須調(diào)用pthread_exit()結(jié)束,否則直到主進(jìn)程函數(shù)退出才釋放。
??示例:
#include
#include
#include
void *start_routine_func(void *arg)
{
int cnt=0;
while (1)
{
printf("子線程運(yùn)行中cnt=%d。。。\n",cnt);
sleep(1);
cnt++;
if(cnt>=3)break;
}
pthread_exit(NULL);//退出線程,釋放堆棧
}
int main()
{
int stat;
pthread_t pth;//線程標(biāo)志符
/*創(chuàng)建子線線程*/
if(pthread_create(&pth,NULL,start_routine_func,NULL)!=0)
{
printf("線程創(chuàng)建失敗\n");
return 0;
}
printf("子線程ID=%lu\n",pth);
/*等待線程退出*/
pthread_join(pth,NULL);
printf("線程退出成功\r\n");
return 0;
}
[xsw@xsw 系統(tǒng)編程]$ gcc pthread.c -lpthread
[xsw@xsw 系統(tǒng)編程]$ ./a.out
子線程ID=3078433648
子線程運(yùn)行中cnt=0。。。
子線程運(yùn)行中cnt=1。。。
子線程運(yùn)行中cnt=2。。。
線程退出成功
?1.3 等待線程結(jié)束pthread_join
int pthread_join(pthread_t thread, void **retval);
函數(shù)功能:
??以阻塞方式等待thread指定線程結(jié)束,當(dāng)函數(shù)返回值,被等待線程的資源被回收。若線程已經(jīng)結(jié)束,則立即返回。并且thread指定的線程必須是joinable(結(jié)合屬性)屬性。
形 參: thread — 線程標(biāo)志符(線程ID)。線程唯一標(biāo)志,類型為:pthread_t
??retval — 用戶定義的指針,用來存儲被等待線程返回的地址
返回值: 成功返回0,失敗返回錯誤編號。
??示例:
#include
#include
#include
void *start_routine_func(void *arg)
{
int cnt=0;
while (1)
{
printf("子線程運(yùn)行中cnt=%d。。。\n",cnt);
sleep(1);
cnt++;
if(cnt>=3)break;
}
pthread_exit(NULL);//退出線程,釋放堆棧
}
int main()
{
int stat;
pthread_t pth;//線程標(biāo)志符
/*創(chuàng)建子線線程*/
if(pthread_create(&pth,NULL,start_routine_func,NULL)!=0)
{
printf("線程創(chuàng)建失敗\n");
return 0;
}
printf("子線程ID=%lu\n",pth);
/*等待線程退出*/
pthread_join(pth,NULL);
printf("線程退出成功\r\n");
return 0;
}
[xsw@xsw 系統(tǒng)編程]$ gcc pthread.c -lpthread
[xsw@xsw 系統(tǒng)編程]$ ./a.out
子線程ID=3078433648
子線程運(yùn)行中cnt=0。。。
子線程運(yùn)行中cnt=1。。。
子線程運(yùn)行中cnt=2。。。
線程退出成功
?1.4 獲取當(dāng)前線程標(biāo)志符pthread_self
??函數(shù)原型:
pthread_t pthread_self(void);
函數(shù)功能:
??獲取線程自身ID。
形 參: 無
返回值: 返回當(dāng)前線程標(biāo)志符。pthread_t類型為unsigned long int,打印應(yīng)%lu。
??示例:
#include
#include
#include
void *start_routine_func(void *arg)
{
printf("子線程ID=%lu運(yùn)行中。。。\n",pthread_self());
pthread_exit(NULL);//退出線程,釋放堆棧
}
int main()
{
int stat;
int i=0;
pthread_t pth;//線程標(biāo)志符
printf("主線程ID=%lu\n",pthread_self());
/*創(chuàng)建5個子線線程*/
for(i=0;i<5;i++)
{
if(pthread_create(&pth,NULL,start_routine_func,NULL)!=0)
{
printf("線程創(chuàng)建失敗\n");
return 0;
}
printf("子線程ID=%lu\n",pth);
}
/*等待線程退出*/
pthread_join(pth,NULL);
printf("線程退出成功\r\n");
return 0;
}
[xsw@xsw 系統(tǒng)編程]$ gcc pthread.c -lpthread
[xsw@xsw 系統(tǒng)編程]$ ./a.out
主線程ID=3078706880
子線程ID=3078703984
子線程ID=3068214128
子線程ID=3057724272
子線程ID=3047234416
子線程ID=3036744560
子線程ID=3068214128運(yùn)行中。。。
子線程ID=3078703984運(yùn)行中。。。
子線程ID=3057724272運(yùn)行中。。。
子線程ID=3047234416運(yùn)行中。。。
子線程ID=3036744560運(yùn)行中。。。
線程退出成功
?1.5 自動清理線程資源
??函數(shù)原型:
//注冊清理函數(shù)
void pthread_cleanup_push(void (*routine)(void *),void *arg);
//釋放清理函數(shù)
void pthread_cleanup_pop(int execute);
函數(shù)功能:
??線程清除處理函數(shù),用于程序異常退出的時候做善后的資源清理。自動釋放資源。
??注:pthread_cleanup_push函數(shù)與pthread_cleanup_pop函數(shù)需要成對調(diào)用。
形 參:
??void (*routine)(void *) — 處理程序函數(shù)入口
??void *arg — 傳遞給處理函數(shù)形參
??int execute — 執(zhí)行的狀態(tài)值,0 – 不調(diào)用清理函數(shù);1 – 調(diào)用清理函數(shù)。
返回值: 無
導(dǎo)致調(diào)用清理函數(shù)條件:
??1.調(diào)用pthread_exit()函數(shù)
??2.Pthread_claenup_pop的形參為1
??注:return不會導(dǎo)致清理函數(shù)調(diào)用。
??示例:
#include
#include
#include
/*線程清理函數(shù)*/
void routine_Clinen(void *arg)
{
printf("arg=%d\n",*(int *)arg);
free(arg);
printf("釋放空間完成\n");
}
/*子線程函數(shù)*/
void *start_routine_func (void *arg)
{
printf("arg=%s,線程運(yùn)行中...\n",arg);
char *p=malloc(4);
*p=100;
//注冊線程清理函數(shù)
pthread_cleanup_push(routine_Clinen,p);
pthread_exit("子線程返回?cái)?shù)據(jù)測試!");//釋放線程堆棧
// return 0;//return終止不會觸發(fā)線程清理函數(shù)
//調(diào)用線程清理函數(shù)
pthread_cleanup_pop(1);
}
int main()
{
/*1.創(chuàng)建線程*/
char buff[]="線程傳入?yún)?shù)測試";
pthread_t thread;
if(pthread_create(&thread,NULL,start_routine_func,buff)!=0)
{
printf("線程創(chuàng)建失敗\n");
return 0;
}
printf("線程ID=%lu\n",pthread_self());
char *p;
pthread_join(thread,(void **)&p);//等待線程退出
printf("子線程返回?cái)?shù)據(jù):%s\n",p);
printf("主線程退出\n");
return 0;
}
[xsw@xsw 系統(tǒng)編程]$ gcc pthread.c -lpthread
[xsw@xsw 系統(tǒng)編程]$ ./a.out
arg=線程傳入?yún)?shù)測試,線程運(yùn)行中...
線程ID=3078866624
arg=100
釋放空間完成
子線程返回?cái)?shù)據(jù):子線程返回?cái)?shù)據(jù)測試!
主線程退出
審核編輯:湯梓紅
-
Linux
+關(guān)注
關(guān)注
87文章
11511瀏覽量
213838 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4381瀏覽量
64898 -
線程
+關(guān)注
關(guān)注
0文章
508瀏覽量
20221 -
線程編程
+關(guān)注
關(guān)注
0文章
5瀏覽量
6194
發(fā)布評論請先 登錄
Linux下線程編程(2)
Linux開發(fā)_采用線程處理網(wǎng)絡(luò)請求
Linux多線程編程基礎(chǔ)知識解析

嵌入式Linux多線程編程
如何對Linux系統(tǒng)多線程進(jìn)行編程呢
linux多線程編程課件
linux多線程編程開發(fā)

關(guān)于Linux下多線程編程技術(shù)學(xué)習(xí)總結(jié)

Linux--線程編程
Linux下的多線程編程
Linux下線程與進(jìn)程的區(qū)別

Linux中多線程編程的知識點(diǎn)

評論