线程相关系统调用(pthread使用)
创建线程(pthread_create())
- 函数原型
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
-
函数功能:创建一个子线程
-
参数
thread
:传出参数,线程创建成功后,子线程的线程ID被写到变量中attr
:设置线程的属性,一般使用默认值(NULL)start_routine
:函数指针,该函数为子线程执行的逻辑代码arg
:给第三个参数使用,传参
- 返回值:成功返回0,失败返回一个错误号,
thread
未定义。
获取错误号的信息:char* strerror(int errnum);
示例1:创建子线程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void* callback(void* arg){
printf("child thread\n");
printf("arg value: %d\n", *(int*)arg);
return NULL;
}
int main(){
pthread_t tid;
int num = 10;
// 创建一个子线程
int ret = pthread_create(&tid, NULL, callback, (void*)&num);
if (ret != 0){
char* errstr = strerror(ret);
printf("error: %s\n", errstr);
}
for (int i = 0; i < 5; i++){
printf("%d\n", i);
}
sleep(1); // 主线程休眠,防止退出
return 0;
}
子线程调用回调函数,输出字符串
终止线程(pthread_exit())
- 函数原型
#include <pthread.h>
void pthread_exit(void *retval);
-
函数功能:终止一个线程,在哪个线程中调用,表示终止哪个线程
-
参数
retval
:传递一个指针,作为一个返回值,可以再pthread_join()
中获取。
- 返回值:无返回值
示例:退出线程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
void* callback(void* arg){
printf("child thread: %ld\n", pthread_self());
return NULL;
}
int main(){
// 创建一个子线程
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if (ret != 0){
char* errstr = strerror(ret);
printf("error: %s\n", errstr);
}
// 主线程
for (int i = 0; i < 5; i++){
printf("%d ", i);
}
printf("\n");
printf("tid: %ld, main thread id : %ld\n", tid, pthread_self());
// 退出主线程,主线程退出不影响其他线程
pthread_exit(NULL);
return 0;
}
连接已终止线程(pthread_join())
- 函数原型
int pthread_join(pthread_t thread, void **retval);
-
函数功能:和一个已经终止的线程进行连接。用于回收子线程的资源。特点:该函数为阻塞函数;调用一次,只能回收一个子线程;一般在主线程中使用。
-
参数
thread
:需要回收的子线程的IDretval
:用于接受子线程退出时的返回值
- 返回值:成功返回0,失败返回错误号。
示例:pthread_join
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
int value = 10;
void* callback(void* arg){
printf("child thread: %ld\n", pthread_self());
sleep(5);
// 不能返回局部变量,因为在线程退出后,栈空间被释放
// int value = 10;
// pthread_exit((void*)&value);
// 等价于 return (void*)&value;
pthread_exit((void*)&value);
}
int main(){
// 创建一个子线程
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if (ret != 0){
char* errstr = strerror(ret);
printf("error: %s\n", errstr);
}
// 主线程
for (int i = 0; i < 5; i++){
printf("%d ", i);
}
printf("\n");
printf("tid: %ld, main thread id : %ld\n", tid, pthread_self());
// 主线程调用pthrad_join 回收子线程
int* thread_retval;
ret = pthread_join(tid, (void*)&thread_retval);
if (ret != 0){
char* errstr = strerror(ret);
printf("error: %s\n", errstr);
}
printf("exit data: %d\n", *thread_retval);
printf("child thread finish...\n");
// 退出主线程,主线程退出不影响其他线程
pthread_exit(NULL);
return 0;
}
线程分离(pthread_detach())
- 函数原型
#include <pthread.h>
int pthread_detach(pthread_t thread);
- 函数功能:分离一个线程, 被分离的线程在终止的时候,会自动释放资源返回给系统。
- 不能多次分离,会产生不可预料的行为。
- 不能去连接一个已经分离的线程。
-
参数:需要分离的线程ID
-
返回值:成功返回0,失败返回错误号。
示例4:线程分离
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void* callback(void* arg){
printf("child thread: %ld\n", pthread_self());
return NULL;
}
int main(){
// 创建一个子线程
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if (ret != 0){
char* errstr = strerror(ret);
printf("error: %s\n", errstr);
}
// 输出主线程和子线程的id
printf("tid: %ld, main thread id : %ld\n", tid, pthread_self());
// 设置子线程分离, 子线程分离后,子线程结束时对应的资源不需主线程释放。
ret = pthread_detach(tid);
if (ret != 0){
char* errstr = strerror(ret);
printf("pthread_detach error: %s\n", errstr);
}
// 设置分离后,对分离子线程进行连接 pthread_join()
ret = pthread_join(tid, NULL);
if (ret != 0){
char* errstr = strerror(ret);
printf("pthread_join error: %s\n", errstr);
}
pthread_exit(NULL);
return 0;
}
分离后的线程不能再次连接
线程取消(pthread_cancel())
- 函数原型
#include <pthread.h>
int pthread_cancel(pthread_t thread);
- 函数功能:取消线程,即终止线程。在调用 pthread_cancel 后,线程并不是立即终止,而是达到
cancellation points
时,才能被取消。cancellation points
为系统规定的一些系统调用,可以理解为从用户区到内核区切换。
示例5:线程取消
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void* callback(void* arg){
printf("child thread: %ld\n", pthread_self());
printf("child thread: ");
for (int i = 0; i < 5; i++){
printf("%d ", i);
}
printf("\n");
return NULL;
}
int main(){
// 创建一个子线程
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if (ret != 0){
char* errstr = strerror(ret);
printf("pthread_create error: %s\n", errstr);
}
// 取消线程
ret = pthread_cancel(tid);
if (ret != 0){
char* errstr = strerror(ret);
printf("pthread_cancel error: %s\n", errstr);
}
printf("main thread: ");
for (int i = 0; i < 5; i++){
printf("%d ", i);
}
printf("\n");
// 输出主线程和子线程的id
printf("tid: %ld, main thread id : %ld\n", tid, pthread_self());
pthread_exit(NULL);
return 0;
}
线程属性
// 线程属性类型 pthread_attr_t
// 初始化线程属性变量
int pthread_attr_init(pthread_attr_t *attr);
// 释放线程属性资源
int pthread_attr_destroy(pthread_attr_t *attr);
// 获取线程分离的状态属性
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
// 设置线程分离的状态属性
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
其他线程函数
// 获取当前线程的线程ID
pthread_t pthread_self(void);
// 比较两个线程ID是否相等
int pthread_equal(pthread_t t1, pthread_t t2);
一键三连是对我最大的鼓励与支持。欢迎关注编程小镇,每天涨一点新姿势😄。