012 Linux_线程控制

前言

本文将会向你介绍线程控制(创建(请见上文),终止,等待,分离)

线程控制

线程终止

pthread_t pthread_self(void); 获取线程自身的ID
在这里插入图片描述

在这里插入图片描述

如果需要只终止某个线程而不终止整个进程,可以有三种方法:
1. 从线程函数return。这种方法对主线程不适用,从main函数return相当于调用exit。
2. 线程可以调用pthread_ exit终止自己。
3. 一个线程可以调用pthread_ cancel终止同一进程中的另一个线程 若是在线程中使用exit()退出,整个进程都会退出

#include <vector>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%x", tid);
    return id;
}
void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 3;
    while(cnt--)
    {
        std::cout << "new thread is running, thread name: " << name << " ,thread id: " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    //return nullptr;	//线程退出
    //exit(13); 	//进程退出
    pthread_exit(nullptr);	//线程退出
    std::cout << "The thread ended ago" << std::endl;
}
int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread-1");
    while(true)
    {
        std::cout << "main: The new thread id is: " <<  ToHex(tid) << std::endl;
        sleep(1);
    }
    return 0;
}

return nullptr:
在这里插入图片描述

exit():
在这里插入图片描述
pthread_exit(nullptr):
在这里插入图片描述
pthread_ cancel:
在这里插入图片描述

线程等待

为什么需要线程等待?
已经退出的线程,其空间没有被释放,仍然在进程的地址空间内。
创建新的线程不会复用刚才退出线程的地址空间

在这里插入图片描述

1. 如果thread线程通过return返回,value_ ptr所指向的单元里存放的是thread线程函数的返回值。
2. 如果thread线程被别的线程调用pthread_ cancel异常终掉,value_ ptr所指向的单元里存放的是常数 (-1)PTHREAD_ CANCELED。
3. 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数。
4. 如果对thread线程的终止状态不感兴趣,可以传NULL给value_ ptr参数

这里只证实后3、4两个结论


#include <vector>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>

void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 3;
    while(cnt--)
    {
        std::cout << "new thread is running, thread name: " << name << " ,thread id: " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    
    //----------------------------------------------------------线程退出
    pthread_exit((void*)"thread-1 over...");
    std::cout << "The thread ended ago" << std::endl;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread-1");
     void *ret = nullptr;
     int n = pthread_join(tid, &ret);
     std::cout << "main thread done" << " ,n: " << n << "info: " << "," << (char*)ret << std::endl;
    return 0;
}

在这里插入图片描述

//等待新线程结束并获取新线程退出的信息(获取新线程退出时的ID、信息、以及退出码)
#include <vector>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
class ThreadReturn
{
public:
    ThreadReturn(pthread_t id, const std::string &info, int code)
        : _id(id)
        , _info(info)
        , _code(code)
    {}
public:
    pthread_t _id;	//线程ID
    std::string _info;	//信息
    int _code;	//返回码
};
//十六进制转换
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%x", tid);
    return id;
}
//线程任务
void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 3;
    while(cnt--)
    {
        std::cout << "new thread is running, thread name: " << name << " ,thread id: " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    //pthread_exit((void*)"thread-1 over...");
    ThreadReturn *ret = new ThreadReturn(pthread_self(), "thread quit normal", 6);
    return ret;
}
int main()
{
    pthread_t tid;
    //创建线程
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread-1");
    void *ret = nullptr;
    //线程等待
    int n = pthread_join(tid, &ret);
    std::cout << "main thread done" << " ,n: " << n << std::endl;
    //安全类型转换
    ThreadReturn *r = static_cast<ThreadReturn *>(ret);
    //输出新线程退出时的参数信息
    std::cout << "main thread get new thread info:" << r->_info << ", " << r->_code << ", " << ToHex(r->_id) << ", " << std::endl;
    delete r;
    return 0;
}

在这里插入图片描述

线程分离

在这里插入图片描述

#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
int gcnt = 3;
void *ThreadRoutine(void *arg)
{
    pthread_detach(pthread_self());
    const char *threadname = (const char *)arg;
    while(true)
    {
        std::cout<< "I am a new thread" << std::endl;
        gcnt--;
        sleep(1);
    }
}
int main()
{
    pthread_t tid1;
    pthread_create(&tid1, NULL, ThreadRoutine, (void*)"thread 1");
    sleep(1);
    if ( pthread_join(tid1, NULL ) == 0 ) 
    {
      std::cout << "pthread wait success\n" << std::endl;
    } 
    else 
    {
      std::cout << "pthread wait failed\n"<< std::endl;
    }
    int n = pthread_cancel(tid1);
    std::cout << "main thread cancel done, " << "n: " << n << std::endl;
    return 0;
}

现象:
线程如果是被分离的,该线程是可以被取消,但是不能被等待
在这里插入图片描述

小结

今日的分享就到这里啦,如果本文存在疏漏或错误的地方还请您能够指出!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fan_558

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值