Linux多线程
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
参数说明
返回值
#include <pthread.h> pthread_t pthread_self(void);
返回值
#include <string.h> char *strerror(int errnum); //不可重入 int strerror_r(int errnum, char* buf, size_t buflen); char* strerror_r(int errnum, char* buf, size_t buflen);
参数说明
返回值
#include <pthread.h> void pthread_exit(void *retval);
参数说明
#include <pthread.h> int pthread_join(pthread_t thread, void **retval);
参数说明
返回值
注意事项
示例-获取返回值
1 #include<stdio.h> 2 #include<pthread.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<unistd.h> 6 7 void showError(const char* str, int nErr) 8 { 9 char pBuf[1024] = {0}; 10 strerror_r(nErr, pBuf, sizeof(pBuf)); 11 printf("%s : %s\n", str, pBuf); 12 } 13 14 void* threadEntry(void* arg) 15 { 16 int* pVal = (int*)malloc(sizeof(int)); 17 printf("address : %p\n", pVal); 18 *pVal = 10; 19 //pthread_exit((void*)pVal); 20 return (void*)pVal; 21 } 22 23 int main(int agc, char** argv) 24 { 25 pthread_t th1; 26 int nRet = pthread_create(&th1, NULL, threadEntry, NULL); 27 if (nRet != 0) 28 { 29 showError("pthread_create", nRet); 30 exit(-1); 31 } 32 33 void* pthread_return_value; 34 pthread_join(th1, &pthread_return_value); 35 printf("address : %p\n", pthread_return_value); 36 printf("Thread return value : %d\n", *(int*)pthread_return_value); 37 return 0; 38 }
gcc main.c -lpthread
address : 0x7fbfb4000b70 address : 0x7fbfb4000b70 Thread return value : 10
#include <pthread.h>
int pthread_detach(pthread_t thread);
参数说明
返回值
#include <pthread.h>
int pthread_cancel(pthread_t thread);
参数说明
返回值
1 #include<stdio.h> 2 #include<pthread.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<unistd.h> 6 7 void* threadEntry(void* arg) 8 { 9 int i = 0; 10 while(1) 11 { 12 printf("child thread : %d\n", i++); 13 sleep(1); 14 } 15 return NULL; 16 } 17 18 int main(int argc, char** argv) 19 { 20 pthread_t th1; 21 pthread_create(&th1, NULL, threadEntry, NULL); 22 sleep(3); 23 int ret = pthread_cancel(th1); 24 if(ret != 0) 25 { 26 printf("failed to cancel thread, error : %d\n", ret); 27 return -1; 28 } 29 printf("cancel thread success!\n"); 30 pause(); 31 return 0; 32 }
child thread : 0 child thread : 1 child thread : 2 cancel thread success!
1 #include<stdio.h> 2 #include<pthread.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<unistd.h> 6 7 void* threadEntry(void* arg) 8 { 9 int i = 0; 10 while(1) 11 { 12 //printf("child thread : %d\n", i++); 13 //sleep(1); 14 } 15 return NULL; 16 } 17 18 int main(int argc, char** argv) 19 { 20 pthread_t th1; 21 pthread_create(&th1, NULL, threadEntry, NULL); 22 sleep(3); 23 printf("send cacel to thread\n"); 24 int ret = pthread_cancel(th1); 25 if(ret != 0) 26 { 27 printf("failed to cancel thread, error : %d\n", ret); 28 return -1; 29 } 30 printf("cancel thread success!\n"); 31 pause(); 32 return 0; 33 }
$ ./a.out send cacel to thread cancel thread success!
$ ps -eLf | grep "./a.out" dxq 30078 18071 30078 0 2 13:29 pts/1 00:00:00 ./a.out dxq 30078 18071 30079 99 2 13:29 pts/1 00:01:19 ./a.out
#include<pthread.h> void pthread_testcancel(void);
1 void* threadEntry(void* arg) 2 { 3 int i = 0; 4 while(1) 5 { 6 pthread_testcancel(); 7 } 8 return NULL; 9 }
#include<pthread.h>
int pthread_equal(pthread_t t1, pthread_t t2);
参数说明
返回值
typedef struct { int detachstate; //线程的分离状态 int schedpolicy; //线程调度策略 struct sched_param schedparam; //线程的调度参数 int inheritsched; //线程的继承性 int scope; //线程的作用域 size_t guardsize; //线程栈末尾的警戒缓冲区大小 int stackaddr_set; //线程的栈设置 void* stackaddr; //线程栈的位置 size_t stacksize; //线程栈的大小 }pthread_attr_t
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr);
参数说明
返回值
#include <pthread.h>
int pthread_attr_destroy(pthread_attr_t *attr);
参数说明
返回值
#include <pthread.h>
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr);
参数说明
返回值
#include <pthread.h> int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);
参数说明
返回值
1 #include <pthread.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 7 void *threadEntry(void *arg) { 8 int i = 0; 9 while (1) 10 { 11 printf("child thread : %d\n", ++i); 12 sleep(1); 13 } 14 return NULL; 15 } 16 17 int main(int argc, char **argv) { 18 pthread_t th1; 19 pthread_attr_t attr; 20 pthread_attr_init(&attr); 21 pthread_attr_setdetachstate(&attr, 1); 22 pthread_create(&th1, &attr, threadEntry, NULL); 23 int i = 0; 24 while(1) 25 { 26 printf("main thread : %d\n", ++i); 27 sleep(1); 28 } 29 pthread_attr_destroy(&attr); 30 pause(); 31 return 0; 32 }
main thread : 1 child thread : 1 child thread : 2 main thread : 2 child thread : 3 main thread : 3 ...
#include <pthread.h> int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);
参数说明
返回值
1 #include <pthread.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 7 void *threadEntry(void *arg) 8 { 9 size_t stackSize = 0; 10 pthread_attr_t attr; 11 pthread_getattr_np(pthread_self(), &attr); 12 pthread_attr_getstacksize(&attr, &stackSize); 13 printf("stack size : %ld\n", stackSize); 14 return NULL; 15 } 16 17 int main(int argc, char **argv) 18 { 19 pthread_t th1; 20 pthread_create(&th1, NULL, threadEntry, NULL); 21 pthread_join(th1, NULL); 22 pause(); 23 return 0; 24 }
stack size : 8388608
$ ulimit -s 8192
$ ulimit -u 7823
#include <pthread.h> //设置栈空间 int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); //获取栈空间 int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
参数说明
返回值
1 #include <pthread.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 const int stackSize = 4 * 1024 * 1024; 7 8 long long calc(int n) 9 { 10 if(n == 1) 11 return 1; 12 13 return n * calc(n - 1); 14 } 15 16 void *threadEntry(void *arg) 17 { 18 size_t stackSize = 0; 19 pthread_attr_t attr; 20 pthread_getattr_np(pthread_self(), &attr); 21 pthread_attr_getstacksize(&attr, &stackSize); 22 printf("stack size : %ld\n", stackSize); 23 24 int n = *(int*)arg; 25 long long* val = (long long*)malloc(sizeof(long long)); 26 *val = calc(n); 27 return (void*)val; 28 } 29 30 int main(int argc, char** argv) 31 { 32 pthread_t th; 33 pthread_attr_t attr; 34 pthread_attr_init(&attr); 35 pthread_attr_setstack(&attr, malloc(stackSize), stackSize); 36 int arg; 37 scanf("%d", &arg); 38 pthread_create(&th, &attr, threadEntry, &arg); 39 40 void* result; 41 pthread_join(th, &result); 42 printf("%d! = %lld\n", arg, *(long long*)result); 43 44 free(result); 45 pthread_attr_destroy(&attr); 46 return 0; 47 }