当前位置: 首页 > news >正文

POSIX线程(pthread)库:线程的终止与管理

在POSIX线程(pthread)库中,线程的终止和管理涉及多个关键函数。以下是关于线程终止的pthread系列函数的详细介绍:


1. pthread_exit:线程主动退出

功能
允许线程主动终止自身,并返回一个退出状态。

函数原型

void pthread_exit(void *retval);

参数

  • retval:线程的退出状态,可由其他线程通过pthread_join获取。

使用场景

  • 线程完成任务后正常退出。

  • 替代return语句,明确表示线程终止。

示例

#include <pthread.h>
#include <stdio.h>

void* thread_func(void* arg) {
    printf("Thread is exiting...\n");
    pthread_exit((void*)42); // 退出状态为42
    return NULL; // 不会执行到这里
}

int main() {
    pthread_t tid;
    void* exit_status;

    pthread_create(&tid, NULL, thread_func, NULL);
    pthread_join(tid, &exit_status);
    printf("Exit status: %ld\n", (long)exit_status); // 输出42
    return 0;
}

2. pthread_cancel:请求取消线程

功能
向目标线程发送取消请求,但线程是否终止取决于其取消状态和类型。

函数原型

int pthread_cancel(pthread_t thread);

参数

  • thread:目标线程的标识符。

返回值

  • 成功返回0,失败返回错误码。

关键点

  • 线程默认启用取消(PTHREAD_CANCEL_ENABLE),但需到达取消点(如sleepread等)才会响应。

  • 可通过pthread_setcancelstatepthread_setcanceltype配置取消行为。

示例

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void* thread_func(void* arg) {
    // 设置取消类型为异步(立即响应)
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    
    while (1) {
        printf("Running...\n");
        sleep(1);
    }
    return NULL;
}

int main() {
    pthread_t tid;
    pthread_create(&tid, NULL, thread_func, NULL);
    sleep(3);
    pthread_cancel(tid); // 发送取消请求
    pthread_join(tid, NULL);
    printf("Thread terminated.\n");
    return 0;
}

3. pthread_join:等待线程终止并回收资源

功能
阻塞当前线程,直到目标线程终止,并获取其退出状态。

函数原型

int pthread_join(pthread_t thread, void **retval);

参数

  • thread:目标线程的标识符。

  • retval:接收线程退出状态的指针。

注意事项

  • 若线程已分离(pthread_detach),则不能再调用pthread_join

  • 必须调用此函数避免僵尸线程(资源泄漏)。

示例

pthread_t tid;
void* exit_status;

pthread_create(&tid, NULL, thread_func, NULL);
pthread_join(tid, &exit_status); // 等待线程结束
printf("Exit code: %d\n", (int)exit_status);

4. 线程清理函数:pthread_cleanup_pushpthread_cleanup_pop 🧹

功能
注册清理函数,在线程被取消或调用pthread_exit时自动执行资源释放。

函数原型

void pthread_cleanup_push(void (*routine)(void*), void *arg);
void pthread_cleanup_pop(int execute);

使用场景

  • 确保动态分配的内存、文件句柄、锁等资源被正确释放。

示例

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void cleanup(void* arg) {
    printf("Cleaning up: %s\n", (char*)arg);
    free(arg); // 释放资源
}

void* thread_func(void* arg) {
    char* data = malloc(100);
    sprintf(data, "Allocated memory");

    pthread_cleanup_push(cleanup, data); // 注册清理函数

    while (1) {
        printf("Working with data: %s\n", data);
        sleep(1);
        // 若在此处被取消,cleanup函数会自动调用
    }

    pthread_cleanup_pop(1); // 弹出并执行清理函数(参数1表示执行)
    return NULL;
}

5. 取消状态与类型设置 ⚙️

  • pthread_setcancelstate:启用或禁用线程取消。

    int pthread_setcancelstate(int state, int *oldstate);
    

    参数state

    • PTHREAD_CANCEL_ENABLE(默认): 允许取消请求。

    • PTHREAD_CANCEL_DISABLE:忽略取消请求。

  • pthread_setcanceltype:设置取消类型。

    int pthread_setcanceltype(int type, int *oldtype);
    

    参数type

    • PTHREAD_CANCEL_DEFERRED(默认): 延迟取消,需到达取消点。

    • PTHREAD_CANCEL_ASYNCHRONOUS:立即取消(可能破坏数据一致性)。


6. pthread_detach:分离线程 🧑‍💻

功能
将线程标记为分离状态,线程终止后自动释放资源,无需pthread_join

函数原型

int pthread_detach(pthread_t thread);

示例

pthread_t tid;
pthread_create(&tid, NULL, thread_func, NULL);
pthread_detach(tid); // 分离线程

总结与最佳实践 📋

  1. 优先协作式终止
    使用共享变量(如volatile或原子变量)通知线程退出循环,而非强制取消。

  2. 慎用pthread_cancel
    强制取消可能导致资源泄漏,仅在必要时使用,并配合清理函数。

  3. 资源管理
    始终通过pthread_cleanup_push/pop或RAII模式确保资源释放。

  4. 分离与合并的选择

    • 需要获取线程结果 → pthread_join

    • 后台任务无需等待 → pthread_detach

  5. 错误检查
    检查pthread_createpthread_join等函数的返回值,处理潜在错误。


通过合理使用上述函数,可以实现线程的安全终止和资源管理,避免常见的多线程问题如死锁、资源泄漏等。

 

相关文章:

  • C#异步方法返回Task<T>的同步调用
  • LLM相关代码笔记
  • 【Docker基础】容器技术详解:生命周期、命令与实战案例
  • Java网络编程实战(多人聊天室-CS模式)
  • ollama加载本地自定义模型
  • 在 Linux 系统(ubuntu/kylin)上安装 Docker
  • 玻璃厂退火炉“温度智囊”:Profinet转ModbusRTU网关
  • 目标检测YOLO实战应用案例100讲- 基于卷积神经网络的小目标检测算法研究与应用
  • 灵霄破茧:仙途启幕 - 灵霄门新篇-(4)
  • linux环境定时重启服务的流程分享
  • 关于FocalLoss 损失函数
  • 【C++算法】54.链表_合并 K 个升序链表
  • Ansible:role企业级实战
  • 4-6记录(B树)
  • 使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第七讲)
  • 【React】副作用 setState执行流程 内置钩子(Effect Callback Reducer)React.memo
  • 从 STP 到 RSTP 再到 MSTP:网络生成树协议的工作机制与发展
  • Docker部署.NetCore8项目
  • 【Axure视频教程】中继器表格轮播含暂停效果
  • 蓝桥杯真题:数字串个数