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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux多线程 pthread用法

發(fā)布時(shí)間:2023/12/18 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux多线程 pthread用法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),void *restrict arg);
Returns: 0 if OK, error number on failure

第一個(gè)參數(shù)為指向線程標(biāo)識(shí)符的指針。

第二個(gè)參數(shù)用來(lái)設(shè)置線程屬性。

第三個(gè)參數(shù)是線程運(yùn)行函數(shù)的起始地址。

第四個(gè)參數(shù)是運(yùn)行函數(shù)的參數(shù)。

當(dāng)創(chuàng)建線程成功時(shí),函數(shù)返回0,若不為0則說(shuō)明創(chuàng)建線程失敗,常見(jiàn)的錯(cuò)誤返回代碼為EAGAIN和EINVAL。前者表示系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過(guò)多了;后者表示第二個(gè)參數(shù)代表的線程屬性值非法.
pthread_create的用法:由于pthread庫(kù)不是Linux系統(tǒng)默認(rèn)的庫(kù),所以在使用pthread_create創(chuàng)建線程時(shí),需要在編譯中請(qǐng)加-lpthread參數(shù),eg:gcc -o test -lpthrea test.c

例1:

#include "pthread.h"
#include "stdio.h"
void* thread_test(void* ptr)
{ while(1)
printf("i am pthread\n");
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, test_thread, NULL);
while(1)
printf("i am main pthread\n");
return 0;
}

例2:

#include
#include
pthread_t id;
int ret;
void thread_1()
{
while(1)
{printf(“I am thread\n”);
sleep(1);
}
}
main()
{ret = pthread_create(&id,NULL,(void*)thread_1,NULL);
if(ret != 0)
printf("Create pthread error!\n");
while(1)
{
printf(“I am main thread\n”);
sleep(2);
}
}

例3:

#include
#include
#include
#include
void *thread_function(void *arg);
char message[] = "Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result); //pthread_join 阻塞執(zhí)行的線程直到某線程結(jié)束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
[root@plinux tmp]# cc -D_REENTRANT -I/usr/include/nptl thread2.c -o thread2 -L/usr/lib/nptl -lpthread
[root@plinux tmp]# ./thread2
thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
pthread_join()
void pthread_exit(void *retval)
int pthread_join(pthread_t pid, void **thread_return)

pthread_join()的調(diào)用者將掛起并等待th線程終止,retval是調(diào)用pthread_exit()的線程(線程ID為pid)的返回值,如果thread_return不為NULL,則*thread_return=retval。

需要注意的是一個(gè)線程僅允許唯一的另一個(gè)線程使用 pthread_join()等待本線程的終止,并且被等待的線程應(yīng)該處于可join狀態(tài),即非DETACHED狀態(tài)。

總結(jié)

以上是生活随笔為你收集整理的linux多线程 pthread用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 一级免费片 | 波多野结衣久久 | 全黄毛片 | 国产h在线 | 91在线无精精品入口 | 色婷婷国产精品综合在线观看 | 人体写真 福利视频 | 亚洲男人天堂影院 | 国产三级理论 | 日本不卡在线 | 国产精品免费看 | 国产三级在线 | 一本之道久久 | 亚洲第一视频在线播放 | 精品亚洲永久免费精品 | 在线免费看黄视频 | 黄色三级图片 | 又粗又大又硬又长又爽 | 一区二区xxx | 无码gogo大胆啪啪艺术 | 国产一区二区三区黄 | 最近中文字幕在线中文高清版 | 深夜av在线 | 亚洲国产视频在线观看 | 日本黄在线 | 贝利弗山的秘密在线观看 | 99这里只有精品视频 | 性欧美丰满熟妇xxxx性仙踪林 | 亚洲在线免费看 | 91丝袜呻吟高潮美腿白嫩 | 日本不卡一区二区三区在线观看 | 日韩av综合 | 欧美激情一区在线 | av成人在线播放 | 91性高潮久久久久久久久 | 午夜在线免费观看视频 | 国产美女久久久久 | 台湾佬美性中文娱乐网 | 无码gogo大胆啪啪艺术 | 精品人妻一区二区三区浪潮在线 | 久草福利在线视频 | 欧美毛片在线观看 | 91麻豆精品国产91久久久更新时间 | 一级黄色片欧美 | 色综合久久久无码中文字幕波多 | 成年人国产 | 一级久久久久久 | 久久一级大片 | 可以直接看的无码av | 96久久| 久久日本视频 | 国内一级视频 | 毛片.com| 国产啊v在线观看 | 久热国产精品 | 一区二区三区在线看 | av免播放器在线观看 | www.成人av | 国产1级片 | 成人h片 | 影音先锋男人站 | 影音先锋国产 | 日日舔夜夜摸 | 成人午夜精品福利免费 | 修女也疯狂3免费观看完整版 | 亚洲日本一区二区三区 | 右手影院亚洲欧美 | 亚洲精品影院在线 | 欧美理论视频 | 91伊人 | 尤物视频在线观看视频 | 亚洲一区二区三区在线看 | 日本三级中文字幕 | 精品国产乱码一区二区 | 欧美丰满熟妇xxxxx | 日本呦呦 | 超碰网站在线观看 | 欧美a级免费 | 国产福利电影在线 | 天天干狠狠干 | 国产一区一一区高清不卡 | www..com黄色| 国产一级淫片a | 欧美日韩一区二区三区免费 | 91久精品 | 97神马影院 | 亚洲欧美自拍一区 | 影音先锋中文字幕人妻 | 久久2018| 综合精品久久 | 欧美性黄色 | 男女啪啪网站 | 日日操天天射 | 99涩涩 | 国产精品一线天粉嫩av | 中日黄色片 | 国产精品一区二区在线观看 | 探花国产在线 | 久久黄色免费网站 |