Linux 连接已经终止的线程 线程的分离 线程取消

连接已经终止的线程

/*
    #include <pthread.h>
    int pthread_join(pthread_t thread, void **retval);
    -功能:连接已经终止的线程(回收子线程的资源)
        阻塞函数,调用一次回收一个子线程
        一般在主线程中使用
    -参数:
        -thread:需要回收的子线程的ID
        -retval:接受子线程退出时的返回值
    返回值:
        -成功0
        -失败 非0 设置错误号
*/
#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>

int value = 10;
void * callback(void* arg) {
    printf("child thread id %ld\n", pthread_self());
    
    pthread_exit((void*) &value);
}

int main() {
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, callback, NULL);
    if(ret != 0) {
        char * strerr = strerror(ret);
        printf("error:%s\n", strerr);
    }

    for(int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
    printf("child thread id %ld, main tid: %ld\n", tid, pthread_self());

    int * thread_returnvalue;
    ret = pthread_join(tid, (void**)&thread_returnvalue);

    if(ret != 0) {
        char* strerr = strerror(ret);
        printf("error: %s\n", strerr);
    }

    printf("exit data : %d\n", *thread_returnvalue);

    printf("child thread id %ld, main tid: %ld\n", tid, pthread_self());
    //主线程退出,其他线程不受影响
    pthread_exit(NULL);

    return 0;
}

线程的分离

/*
    #include <pthread.h>
    int pthread_detach(pthread_t thread);
    功能:分离一个线程。被分离的线程在终止的时候会自动释放资源给系统
         不能多次分离,会产生不可预料的行为。
         不能去连接一个已经分离的线程,会报错
    参数:需要分离的线程的ID
    返回值:成功:0
            失败:错误号
*/

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

void* callback(void* arg) {
    printf("child thread id:%ld", pthread_self());
    return NULL;
}

int main() {
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, callback, NULL);
    if(ret != 0) {
        char* strerr = strerror(ret);
        printf("error: %s\n", strerr);
    }

    printf("tid: %ld, main thread id:%ld\n", tid, pthread_self());

    pthread_detach(tid);

    ret = pthread_join(tid, NULL);
    if(ret != 0) {
        char* strerr = strerror(ret);
        printf("error: %s\n", strerr);
    }
    pthread_exit(NULL);

    return 0;
}

线程取消

/*
    #include <pthread.h>
    int pthread_cancel(pthread_t thread);
    功能:取消线程(让线程终止)
        取消线程,可以终止某个线程的运行
        但是不是立马取消,而是当子线程执行到一个取消点,线程才会终止。
        取消点:系统规定好的一些系统调用,我们可以粗略的理解为从用户区到内核区的切换的位置
    参数:线程id
*/
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

void* callback(void* arg) {
    printf("child thread id:%ld", pthread_self());
    for(int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
    return NULL;
}

int main() {
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, callback, NULL);
    if(ret != 0) {
        char* strerr = strerror(ret);
        printf("error: %s\n", strerr);
    }

    pthread_cancel(tid);

    for(int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }

    printf("tid: %ld, main thread id:%ld\n", tid, pthread_self());
 
    pthread_exit(NULL);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值