pthread_create()函數詳解
pthread_create是類Unix操作系統(Unix、Linux、Mac OS X等)的創建線程的函數。它的功能是創建線程(實際上就是確定調用該線程函數的入口點),在線程創建以后,就開始運行相關的線程函數。
頭文件:
#include<pthread.h>
函數原型:
int pthread_create (pthread_t * tidp, const pthread_attr_t * attr, void * (*start_rtn)(void*), void *arg);
若線程創建成功,則返回0。若線程創建失敗,則返回出錯編號,并且*thread中的內容是未定義的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#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; } |
結果展示:
到此這篇關于C語言 pthread_create() 函數講解的文章就介紹到這了,更多相關C語言 pthread_create()內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/ruigelwang/p/13163304.html