[Linux]pthread学习笔记

本文详细解析Linux线程的joinable与unjoinable状态,阐述pthread_create、pthread_join、pthread_exit与线程清理处理程序pthread_cleanup_push、pthread_cleanup_pop的使用方法与应用场景。通过实例代码演示如何正确地管理线程生命周期与资源回收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/zjc156m/article/details/9028643

pthread_detach(pthread_self())
linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,
如果线程是joinable状态,当线程函数自己返回退出时或pthread_exit时都不会释放线程所占用堆栈和线程描述符(总计8K多)。只有当你调用了pthread_join之后这些资源才会被释放。
若是unjoinable状态的线程,这些资源在线程函数退出时或pthread_exit时自动会被释放。

unjoinable属性可以在pthread_create时指定,或在线程创建后在线程中pthread_detach自己, 如:pthread_detach(pthread_self()),将状态改为unjoinable状态,确保资源的释放。或者将线程置为 joinable,然后适时调用pthread_join.

其实简单的说就是在线程函数头加上 pthread_detach(pthread_self())的话,线程状态改变,在函数尾部直接pthread_exit线程就会自动退出。省去了给线程擦屁股的麻烦

eg:

 pthread_t tid;
 int status = pthread_create(&tid, NULL, ThreadFunc, NULL);
 if(status != 0)
 {
  perror("pthread_create error");
 }
 pthread_detach(tid);



转自:http://www.cnblogs.com/xfiver/archive/2013/01/23/2873725.html
#include <pthread.h>
//新建线程
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *),void *restrict arg);
 
//线程终止
void pthread_exit(void *rval_ptr);//线程自身主动退出
int pthread_join(pthread_t tid, void **rval_ptr);//其他线程阻塞自身,等待tid退出
 
//线程清理
void pthread_cleanup_push(void (*rtn)(void *),void *arg);
void pthread_cleanup_pop(int execute);

补充说明:

1. 线程创建

pthread_create()函数返回值0,表示创建成功,线程id保存载tidp中;失败则返回非零,需自行处理,不会修改errno值

2. 线程终止

a. 任一线程调用exit, _Exit, _exit都将导致整个进程终止;

b. 单个线程退出方式有三种:

  1> 线程执行函数start_rtn()中使用return返回,返回值为线程退出码;

  2> 被同一个进程的其他线程使用pthread_cancel()取消;

  3> 线程自身调用了pthread_exit();

说明:pthread_join(pthread_t tid, void **rval_ptr)函数会阻塞调用线程,直到tid线程通过上述三种方式终止退出,且return/pthread_exit()方式会设置相应线程退出码rval_ptr,而pthread_cancel()取消的线程,将退出码设置为PTHREAD_CANCELED.

3. 线程清理处理程序(thread cleanup handler)

3.a> pthread_cleanup_push()与pthread_cleanup_pop()均为<pthread.h>中实现的宏定义,具体实现如下:

pthread_cleanup_push and pthread_cleanup_pop are macros and must always
   be used in matching pairs at the same nesting level of braces.  */
#  define pthread_cleanup_push(routine, arg) \
  do {                                        \
    __pthread_cleanup_class __clframe (routine, arg)
 
/* Remove a cleanup handler installed by the matching pthread_cleanup_push.
   If EXECUTE is non-zero, the handler function is called. */
#  define pthread_cleanup_pop(execute) \
    __clframe.__setdoit (execute);                        \
  }while (0)

 可见push/pop中的{/}是一一对应的,因此pthread_cleanup_push/pop()也应一一对应出现,否则编译出错。

3.b> 当线程执行下列之一操作时调用清理函数,thread_cleanup_push由栈结构实现,注意清理程序调用的顺序,先入后出。

  1: 调用pthread_exit()时,而直接return不会出发清理函数;

  2: 相应取消请求pthread_cancel()时;

  3: 使用非零execute参数调用pthread_cleanup_pop()时;

尤其需注意pthread_cleanup_pop()参数不同及此语句所处位置不同而有不同效果。

看此代码实例,注意return或pthread_exit()位置不同导致pthread_cleanup_pop()不同参数的效果变化。

#include <pthread.h>
void testPointerSize()
{
    void *tret;
    printf("size of pointer in x86-64:%d\n",sizeof(tret)); 
    //result is 8 in x86-64.
    //which is 4 in x86-32.
 
    printf("size of int in x86-64:%d\n",sizeof(int));  
    //result is 4 in x86-64.
    //which is also 4 in x86-32.
}
void cleanup(void *arg)
{
    printf("cleanup:%s\n",(char *)arg);
}
void * thr_fn1(void *arg)
{
    printf("thread 1 start\n");
    pthread_cleanup_push(cleanup,"thread 1 first handler");
    pthread_cleanup_push(cleanup,"thread 1 second handler");
    if(arg)
        return ((void *)1);//arg !=0 ,return here.
//  return here will not triger any cleanup.
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(1);
    return ((void *)2);//will not run this
}
void * thr_fn2(void *arg)
{
    printf("thread 2 start\n");
    pthread_cleanup_push(cleanup,"thread 2 first handler");
    pthread_cleanup_push(cleanup,"thread 2 second handler");
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(1);
    return ((void *)2);
//  return here can triger cleanup second handler;
}
 
void * thr_fn3(void *arg)
{
    printf("thread 3 start\n");
    pthread_cleanup_push(cleanup,"thread 3 first handler");
    pthread_cleanup_push(cleanup,"thread 3 second handler");
    if(arg)
        pthread_exit((void *)3);
    //pthread_exit() here will triger both cleanup first&second handler.
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(0);
    pthread_exit((void *)3);//wont run this
}
void * thr_fn4(void *arg)
{
    printf("thread 4 start\n");
    pthread_cleanup_push(cleanup,"thread 4 first handler");
    pthread_cleanup_push(cleanup,"thread 4 second handler");
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(0);
    pthread_exit((void *)4);
    //pthread_exit() here will triger cleanup second handler.
}
 
int main(void)
{
    testPointerSize();
    int err;
    pthread_t tid1, tid2, tid3, tid4;
    void *tret;
     
    err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
    err = pthread_join(tid1,&tret);
    printf("thread 1 exit code %d\n",(int)tret);
     
    err = pthread_create(&tid2, NULL, thr_fn2, (void *)2);
    err = pthread_join(tid2, &tret);
    printf("thread 2 exit code %d\n",(int)tret);
 
    err = pthread_create(&tid3, NULL, thr_fn3, (void *)3);
    err = pthread_join(tid3,&tret);
    printf("thread 3 exit code %d\n",(int)tret);
     
    err = pthread_create(&tid4, NULL, thr_fn4, (void *)4);
    err = pthread_join(tid4, &tret);
    printf("thread 4 exit code %d\n",(int)tret);
}

 运行结果:

[root@hello testData]# ./test
size of pointer in x86-64:8
size of int in x86-64:4
thread 1 start
thread 1exit code 1
thread 2 start
cleanup:thread 2 first handler
thread 2exit code 2
thread 3 start
cleanup:thread 3 second handler
cleanup:thread 3 first handler
thread 3exit code 3
thread 4 start
cleanup:thread 4 second handler
thread 4exit code 4

  由上述测试程序总结如下:

1> push与pop间的return,将导致清理程序不被触发;

2> 位于pop之后return,由pop的参数确定是否触发清理程序,非零参数触发,零参数不触发;

3> push/pop间的pthread_exit(),将触发所有清理函数;

4>位于pop之后的pthread_exit()时,pop参数决定是否触发清理程序;

其实,上述四种情况只是测试验证了前文3.b所说三个条件,加深理解。




内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值