C語言 pthread_create() 函數(shù)講解
pthread_create()函數(shù)詳解
pthread_create是類Unix操作系統(tǒng)(Unix、Linux、Mac OS X等)的創(chuàng)建線程的函數(shù)。它的功能是創(chuàng)建線程(實際上就是確定調(diào)用該線程函數(shù)的入口點),在線程創(chuàng)建以后,就開始運行相關(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)建失敗,則返回出錯編號,并且*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語言 pthread_create() 函數(shù)講解的文章就介紹到這了,更多相關(guān)C語言 pthread_create()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++使用循環(huán)計算標(biāo)準(zhǔn)差的代碼實現(xiàn)
在C++中,計算標(biāo)準(zhǔn)差可以使用循環(huán)來實現(xiàn),本文給大家介紹了一個示例代碼,演示了如何使用循環(huán)計算標(biāo)準(zhǔn)差,文中示例代碼介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-12-12
C語言如何實現(xiàn)一些算法或者函數(shù)你知道嗎
這篇文章主要為大家詳細介紹了C語言實現(xiàn)一些算法或者函數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
VS2019配置opencv詳細圖文教程和測試代碼的實現(xiàn)
這篇文章主要介紹了VS2019配置opencv詳細圖文教程和測試代碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

