1. 创建线程 pthread_create()
1
| int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
|
参数1: thread:指向pthread_create类型的指针,用于引用新创建的线程。
参数2: attr:用于设置线程的属性,一般不需要特殊的属性,所以可以简单地设置为NULL。
参数3: start_routine:传递新线程所要执行的函数地址。
参数4: arg:新线程所要执行的函数的参数。
return: 成功返回0, 失败返回错误代码
2. 获取自己的线程ID pthread_self()
3. 线程退出
- 执行完成后隐式退出
- 由线程本身显式调用pthread_exit函数
1
| pthread_exit(void * retval);
|
- 被其他线程用pthread_cancel函数终止
1
| pthread_cancel(pthread_t thread);
|
- 如果一个线程要等待另一个线程的终止,可以使用pthread_join函数,该函数的作用是调用pthread_join的线程将被挂起直到线程ID为参数thread的线程终止
1
| pthread_join(pthread_t thread, void** threadreturn);
|
Author:
Qin Peng
License:
Copyright (c) 2020 BY QPWLKQ LICENSE
Slogan:
每一个不曾起舞的日子, 都是对生命的辜负