C語言 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)建以后,就開始運(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)建失敗,則返回出錯編號,并且*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)文章
Qt?QtCreator添加自定義注釋的實(shí)現(xiàn)方法
在寫代碼的時(shí)候我們?yōu)榱艘?guī)范化,一般會加文件注釋、類注釋和函數(shù)注釋,本文主要介紹了Qt?QtCreator添加自定義注釋的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
C++ 中"priority_queue" 優(yōu)先級隊(duì)列實(shí)例詳解
這篇文章主要介紹了C++ 中"priority_queue" 優(yōu)先級隊(duì)列實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
C++實(shí)現(xiàn)判斷字符串是否回文實(shí)例解析
這篇文章主要介紹了C++實(shí)現(xiàn)判斷字符串是否回文,其中采用了數(shù)據(jù)結(jié)構(gòu)中棧以及過濾字符等技術(shù),,需要的朋友可以參考下2014-07-07
C++?Protobuf實(shí)現(xiàn)接口參數(shù)自動校驗(yàn)詳解
用C++做業(yè)務(wù)發(fā)開的同學(xué)是否還在不厭其煩的編寫大量if-else模塊來做接口參數(shù)校驗(yàn)?zāi)兀拷裉?,我們就模擬Java里面通過注解實(shí)現(xiàn)參數(shù)校驗(yàn)的方式來針對C++?protobuf接口實(shí)現(xiàn)一個(gè)更加方便、快捷的參數(shù)校驗(yàn)自動工具,希望對大家有所幫助2023-04-04

