Linux 多线程常用的API:pthread_self、pthread_exit、pthread_join、pthread_detach和pthread_cancel

 1.pthread_self

#include <pthread.h>
pthread_t pthread_self(void);
功能:获取当前线程的线程号
参数:
    @无
返回值:总是会成功,返回调用线程的线程号(在哪个线程内使用,就返回哪个线程的线程号)

实例:

#include <head.h>
void * pthread(void *args){
    pthread_t  a = (pthread_t)args;//typedef unsigned long int pthread_t;
  printf("子线程:主线程进程号 = %#lx,子线程进程号 = %#lx\n",a,pthread_self());
}
int main(int argc,const char * argv[])
{
    pthread_t tid;//typedef unsigned long int pthread_t;
    if((errno = pthread_create(&tid,NULL,pthread,(void *)pthread_self()))!=0){
        perror("pthread_create error");
        exit(-1);
    }
    printf("主线程:主线程进程号 = %#lx,子线程进程号 = %#lx\n",pthread_self(),tid);
     sleep(1);// 不能让进程退出,如果进程退出了,线程没有执行的内存了
    return 0;
}

2.pthread_exit

void pthread_exit(void *retval);
功能:退出当前的线程(在线程内如果调用了exit/_exit会让进程退出)
参数:
    @retval:返回线程退出时候的状态值
返回值:无

实例:

#include <head.h>
void *pthread(void *args)
{
    printf("子线程:11111111\n");
    pthread_exit(NULL);//让线程退出
    printf("子线程:2222222222\n");//此句话线程退出了 不打印
}
int main(int argc, const char *argv[])
{
    pthread_t tid; // typedef unsigned long int pthread_t;
    if ((errno = pthread_create(&tid, NULL, pthread, NULL)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }
    sleep(1);//先不让进程退出,如果退出了 线程就没有执行内存了
    return 0;
}

3.pthread_join

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
功能:阻塞等待回收子线程资源(结合态的线程)
参数:
    @thread:线程号
 @retval:接收线程退出时候的状态值
返回值:成功返回0,失败返回错误码

实例:

#include <head.h>
void *pthread(void *args)
{
    printf("子线程\n");
    static int retval =  12345;
    printf("子retval = %p\n",&retval);
    sleep(1);
    pthread_exit((void *)&retval);//让线程退出
    printf("2222222222\n");//此句话线程退出了 不打印
}
int main(int argc, const char *argv[])
{
    pthread_t tid; // typedef unsigned long int pthread_t;
    if ((errno = pthread_create(&tid, NULL, pthread, NULL)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }
    int *retval;//这个指针会在pthread_join内部指向thread中的reaval地址
    pthread_join(tid,(void **)&retval); // 阻塞等待回收子线程的资源
    printf("主retval = %p\n",retval);
    printf("retval = %d\n",*retval);
    return 0;
}

4.pthread_detach

线程的状态分为两种:结合态和分离态

结合态:结合态的线程需要使用到pthread_join来回收资源,线程默认的状态就是结合态

分离态:分离态的线程结束后资源会被自动回收。

#include <pthread.h>

int pthread_detach(pthread_t thread);
功能:标记thread线程为分离态
参数:
    @thread:线程号
返回值:成功返回0,失败返回错误码

实例:

#include <head.h>
void *thread(void *args)
{
    printf("子进程\n");
    pthread_detach(pthread_self()); // 将线程标记为分离态
    sleep(3);
    pthread_exit(NULL); // 让线程退出
}
int main(int argc, const char *argv[])
{
    pthread_t tid;
    if ((errno = pthread_create(&tid, NULL, thread, NULL)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }

//pthread_join(tid,NULL);//不用使用到pthread_join回收
    while (1){};
    return 0;
}

5.pthread_cancel

#include <pthread.h>

int pthread_cancel(pthread_t thread);
功能:给thread线程发送取消的信号
参数:
    @thread:线程号
返回值:成功返回0,失败返回错误码

int pthread_setcancelstate(int state, int *oldstate);
功能:设置线程能够被取消
参数:
    @state:
  PTHREAD_CANCEL_ENABLE:可以被取消(线程默认设置)
        PTHREAD_CANCEL_DISABLE:不可以被取消
 @oldstate:返回设置前的状态值
返回值:成功返回0,失败返回非0的错误码
        
int pthread_setcanceltype(int type, int *oldtype);
功能:设置线程何时被取消
参数:
    @type:
  PTHREAD_CANCEL_DEFERRED:延时取消,需要找到取消点,才能取消线程
            (比如:当系统调用用内核空间返回到用户空间的时候就是一个取消点)
        PTHREAD_CANCEL_ASYNCHRONOUS:立即被取消
 @oldtype:返回设置前的类型值
返回值:成功返回0,失败返回非0的错误码

实例1:

#include <head.h>
int a;
void *thread1(void *args)
{
    printf("子线程1...\n");
    while (1)
    {
        sleep(1);
    }
}
void *thread2(void *args)
{
    printf("子线程2...\n");
    sleep(10);
    pthread_cancel((pthread_t)args);
    while (1);
}
int main(int argc, const char *argv[])
{
    pthread_t tid1, tid2;
    if ((errno = pthread_create(&tid1, NULL, thread1, NULL)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }
    if ((errno = pthread_create(&tid2, NULL, thread2, (void *)tid1)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }
    printf("主线程....\n");
    while (1)
        ;
    return 0;
}

 主线程号LWP27876(第四列)       线程1    27877  线程2   27878     10秒后查看    线程1被取消

实例2: 

#include <head.h>
int a;
void *thread1(void *args)
{
   // pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);//线程可以被取消 线程默认设置  与实例1结果相同
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);//线程不可以被取消
    printf("子线程1...\n");
    while (1)
    {
        sleep(1);
    }
}
void *thread2(void *args)
{
    printf("子线程2...\n");
    sleep(10);
    pthread_cancel((pthread_t)args);
    while (1)
    {
    };
}
int main(int argc, const char *argv[])
{
    pthread_t tid1, tid2;
    if ((errno = pthread_create(&tid1, NULL, thread1, NULL)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }
    if ((errno = pthread_create(&tid2, NULL, thread2, (void *)tid1)) != 0)
    {
        perror("pthread_create error");
        exit(-1);
    }
    printf("主线程....\n");
    while (1)
        ;
    return 0;
}

 主线程号LWP32519(第四列)       线程1   32520  线程2   32521     10秒后查看    线程1没有被取消 

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了pthread_detach(pthread_self())的用法,它将线程的状态改为unjoinable状态,确保资源的释放。这个函数通常在线程内部调用。引用\[2\]和引用\[3\]分别给出了使用pthread_detachpthread_join的不同顺序的示例代码。 在引用\[2\]中,先调用了pthread_detach(pthread_self()),然后在主线程中调用pthread_join(tid, NULL)。这种情况下,pthread_join会返回错误码22,表示无效的参数。因为线程已经被设置为unjoinable状态,所以无法再使用pthread_join来等待线程的结束。 在引用\[3\]中,先调用了pthread_join(tid, NULL),然后在线程内部调用pthread_detach(pthread_self())。这种情况下,pthread_join会成功返回0,表示成功等待线程的结束。因为线程已经被设置为unjoinable状态,所以即使在主线程中调用pthread_join也不会出错。 综上所述,如果先调用pthread_detach(pthread_self()),再调用pthread_join会导致pthread_join返回错误码22,表示无效的参数。而如果先调用pthread_join,再调用pthread_detach(pthread_self()),则不会出错。 #### 引用[.reference_title] - *1* [【彻底弄懂】linuxpthread_detach()的作用](https://blog.csdn.net/Set_Mode/article/details/121439463)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [linux线程 (1)——初始线程与线程控制](https://blog.csdn.net/m0_59337025/article/details/127989768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值