linux下线程中pthread_detach与pthread_join区别

一.linux下线程pthread_detach的示例

1.简单示例

#include <pthread.h>

#include <stdio.h>

#include <unistd.h>

void* thread_function(void* arg) {

printf("线程正在运行\n");

sleep(2);

printf("线程结束\n");

return NULL;

}

int main() {

pthread_t thread;

// 创建线程

pthread_create(&thread, NULL, thread_function, NULL);

// 将线程设置为分离状态

pthread_detach(thread);

// 注意:这里不能再使用 pthread_join(thread, NULL),因为线程已经分离

// 主线程等待一段时间,以便观察分离线程的输出

sleep(3);

return 0;

}

2.pthread_detach实际应用

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

#include <string.h>

// 网络服务器场景:为每个客户端连接创建分离线程

void* handle_client(void* arg) {

int client_fd = *(int*)arg;

// 处理客户端请求

printf("处理客户端连接 %d\n", client_fd);

sleep(1); // 模拟处理时间

printf("客户端 %d 处理完成\n", client_fd);

return NULL;

}

int main() {

// 模拟多个客户端连接

int client_connections\[\] = {101, 102, 103, 104, 105};

int num_clients = sizeof(client_connections) / sizeof(client_connections0);

pthread_t threadsnum_clients;

for(int i = 0; i < num_clients; i++) {

printf("接受客户端连接: %d\n", client_connectionsi);

if(pthread_create(&threadsi, NULL, handle_client, &client_connectionsi) != 0) {

perror("创建线程失败");

continue;

}

// 分离线程,让它们独立运行

pthread_detach(threadsi);

}

// 主线程可以继续接受新连接

printf("主线程继续监听新连接...\n");

sleep(3); // 模拟继续工作

return 0;

}

3.pthread_join应用简单示例

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

// 线程函数

void* worker(void* arg) {

int id = *(int*)arg;

printf("工作线程 %d: 开始工作\n", id);

// 模拟工作(耗时操作)

for(int i = 1; i <= 3; i++) {

printf("工作线程 %d: 第 %d 步\n", id, i);

sleep(1);

}

printf("工作线程 %d: 工作完成\n", id);

// 返回计算结果

int* result = malloc(sizeof(int));

*result = id * 100;

return (void*)result;

}

int main() {

pthread_t thread1, thread2;

int id1 = 1, id2 = 2;

int* result1;

int* result2;

printf("主线程: 创建线程\n");

// 创建线程1

if(pthread_create(&thread1, NULL, worker, &id1) != 0) {

perror("创建线程1失败");

return 1;

}

// 创建线程2

if(pthread_create(&thread2, NULL, worker, &id2) != 0) {

perror("创建线程2失败");

return 1;

}

printf("主线程: 等待线程完成...\n");

// 等待线程1完成

if(pthread_join(thread1, (void**)&result1) != 0) {

perror("等待线程1失败");

} else {

printf("主线程: 线程1返回结果 = %d\n", *result1);

free(result1); // 释放线程分配的内存

}

// 等待线程2完成

if(pthread_join(thread2, (void**)&result2) != 0) {

perror("等待线程2失败");

} else {

printf("主线程: 线程2返回结果 = %d\n", *result2);

free(result2); // 释放线程分配的内存

}

printf("主线程: 所有线程完成,程序结束\n");

return 0;

}

二.pthread_detach与pthread_join区别

1.int pthread_detach(pthread_t thread);

作用:将线程标记为"分离状态"

参数:thread - 要分离的线程ID

返回值:成功返回0,失败返回错误码

****常见的错误是:****尝试分离一个已经分离的线程,或者线程ID无效。

2.pthread_detach与pthread_join的区别:

pthread_join:同步等待线程结束(阻塞),并回收资源,获取返回值。

pthread_detach:异步执行(非阻塞),线程结束后自动回收资源,无法获取返回值。

总结:

pthread_detach(threadsi); 用于将线程threadsi设置为分离状态,这样当线程结束时,系统会自动回收其资源,无需其他线程显式地等待它结束。这适用于那些不需要同步等待结果的后台任务。

相关推荐
毕竟是shy哥7 小时前
vs code连接远程服务器docker环境及配置免密登录
服务器·docker·vs code
徐小黑ACG8 小时前
nginx配置文件
linux·服务器·nginx
新时代牛马8 小时前
Linux VFS 完整篇:从path_openat、dentry/inode 到page cache 与挂载排障
linux·运维·服务器
java_logo8 小时前
Docker 部署 Rocky Linux:轻松搭建 RHEL 兼容企业级基础镜像平台
linux·docker·容器·rocky linux·基础镜像·轩辕镜像·rhel 兼容
拂拉氏8 小时前
【知识讲解】 Linux虚拟地址空间认识
linux·虚拟地址空间
cloudfantasy10 小时前
docker直接二进制安装,非编译。KylinV10SP3 aarch64有效
运维·docker
新时代牛马10 小时前
Linux 驱动中断与定时完整篇:从 request_threaded_irq 到hrtimer 选型与排障
linux·运维·服务器
考虑考虑11 小时前
nginx打印请求日志
运维·后端·nginx
数字补给站12 小时前
Windows 10 + Hyper-V + Terraform + cloud-init:批量产出 3 台 Ubuntu 的实践笔记
运维·ai编程
分支预测失败12 小时前
RISC-V 核间中断 IPI 实战:从 MSIP 寄存器到 IMSIC 消息投递
linux·后端