C語(yǔ)言 pthread_create() 函數(shù)講解
pthread_create()函數(shù)詳解
pthread_create是類Unix操作系統(tǒng)(Unix、Linux、Mac OS X等)的創(chuàng)建線程的函數(shù)。它的功能是創(chuàng)建線程(實(shí)際上就是確定調(diào)用該線程函數(shù)的入口點(diǎn)),在線程創(chuàng)建以后,就開(kāi)始運(yùn)行相關(guān)的線程函數(shù)。
頭文件:
#include<pthread.h>
函數(shù)原型:
int pthread_create (pthread_t * tidp, const pthread_attr_t * attr, void * (*start_rtn)(void*), void *arg);
若線程創(chuàng)建成功,則返回0。若線程創(chuàng)建失敗,則返回出錯(cuò)編號(hào),并且*thread中的內(nèi)容是未定義的。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int) pid, (unsigned int) tid, (unsigned int) tid);
}
void * thr_fn(void *arg)
{
printids("new thread: ");
return NULL;
}
int main()
{
int err;
pthread_t ntid;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if(err != 0)
{
printf("Can't create thread: %s\n", strerror(err));
}
printids("main thread");
pthread_join(ntid, NULL);
return EXIT_SUCCESS;
}
結(jié)果展示:

到此這篇關(guān)于C語(yǔ)言 pthread_create() 函數(shù)講解的文章就介紹到這了,更多相關(guān)C語(yǔ)言 pthread_create()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++使用循環(huán)計(jì)算標(biāo)準(zhǔn)差的代碼實(shí)現(xiàn)
在C++中,計(jì)算標(biāo)準(zhǔn)差可以使用循環(huán)來(lái)實(shí)現(xiàn),本文給大家介紹了一個(gè)示例代碼,演示了如何使用循環(huán)計(jì)算標(biāo)準(zhǔn)差,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12
C語(yǔ)言如何實(shí)現(xiàn)一些算法或者函數(shù)你知道嗎
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)一些算法或者函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
win32 api實(shí)現(xiàn)2048游戲示例
這篇文章主要介紹了win32 api實(shí)現(xiàn)2048游戲示例,需要的朋友可以參考下2014-05-05
C++無(wú)痛實(shí)現(xiàn)日期類的示例代碼
凡是要寫類必須要提到六大默認(rèn)成員(六位大爺):構(gòu)造函數(shù)、析構(gòu)函數(shù)、拷貝構(gòu)造函數(shù)、賦值重載函數(shù)、取地址重載函數(shù)(包括const對(duì)象和普通對(duì)象);那么這次的日期類又需要伺候哪幾位大爺呢?本文就來(lái)詳細(xì)說(shuō)說(shuō)2022-10-10
VS2019配置opencv詳細(xì)圖文教程和測(cè)試代碼的實(shí)現(xiàn)
這篇文章主要介紹了VS2019配置opencv詳細(xì)圖文教程和測(cè)試代碼的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

