Linux——多线程(三)

简介: Linux——多线程(三)

线程的终止

之前讲过一个exit的函数,这个函数是让进程终止,而不是线程,如果当前进程中任何一个执行流调用了exit函数,那么当前进程都会退出。

那么如何终止一个线程呢?函数中的return可以,还有一个函数可以:

参数默认设置为nullptr就可以了。

#include <iostream>
#include <pthread.h>
#include <cassert>
#include <string>
#include <vector>
#include <unistd.h>
#include <cstdlib>
using namespace std;
struct ThreadData
{
    pthread_t tid;
    char buffer[64];
};
void *pthread_routine(void* args)
{
    ThreadData* p = static_cast<ThreadData*>(args);
    while(true)
    {
        cout << "new " << p->buffer << endl;
        sleep(1);
        pthread_exit(nullptr);//线程终止
    }
    delete p;
}
int main()
{
#define NUM 10
    for(int i = 0; i < NUM; i++)
    {
        ThreadData *p = new ThreadData;
        char buffer[64];
        snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i);
        pthread_create(&p->tid, nullptr, pthread_routine, p);
        //sleep(1);
    }
    while(true)
    {
        cout << "main thread" << endl;
        sleep(1);
    }
    return 0;
}

线程的等待

线程也是要被等待的,如果不等待会造成类似于僵尸进程的问题——内存泄漏。

线程被等待的原因:

1.获取新线程退出的信息。

2.回收新线程对应PCB内核资源等,防止内存泄漏。(这里暂时无法查看)

这是等待线程的函数:

第一个参数是线程的id,第二个参数暂时设置为nullptr。

#include <iostream>
#include <pthread.h>
#include <cassert>
#include <string>
#include <vector>
#include <unistd.h>
#include <cstdlib>
using namespace std;
struct ThreadData
{
    pthread_t tid;
    char buffer[64];
};
void *pthread_routine(void* args)
{
    ThreadData* p = static_cast<ThreadData*>(args);
    while(true)
    {
        cout << "new " << p->buffer << endl;
        sleep(1);
        pthread_exit(nullptr);
    }
}
int main()
{
    vector<ThreadData*> arr;
#define NUM 10
    for(int i = 0; i < NUM; i++)
    {
        ThreadData *p = new ThreadData;//这里每个创建的起始地址都不相同
        char buffer[64];
        snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i);
        pthread_create(&p->tid, nullptr, pthread_routine, p);
        arr.push_back(p);
        //sleep(1);
    }
    for(auto& e : arr)
    {
        cout << "等待线程id:" << e->tid << "成功" << endl;
        int n = pthread_join(e->tid, nullptr);//阻塞等待
        assert(n==0);
        delete e;
    }
    while(true)
    {
        cout << "main thread" << endl;
        sleep(1);
    }
    return 0;
}

那么,该函数的第二个参数是什么呢?其实是线程的返回值。(输出型参数)

这里返回的是一个void*,输出型参数用一个void**才能传回。

#include <iostream>
#include <pthread.h>
#include <cassert>
#include <string>
#include <vector>
#include <unistd.h>
#include <cstdlib>
using namespace std;
struct ThreadData
{
    int num;//线程的编号
    pthread_t tid;
    char buffer[64];
};
void *pthread_routine(void* args)
{
    ThreadData* p = static_cast<ThreadData*>(args);
    while(true)
    {
        cout << "new " << p->buffer << endl;
        sleep(1);
        pthread_exit(nullptr);
    }
    return (void*)p->num;//这里返回的是一个整数,这里强制转换成void*就相当于将四字节的内容写入了八字节的空间里,void* ret = (void*)p->num;
}
int main()
{
    vector<ThreadData*> arr;
#define NUM 10
    for(int i = 0; i < NUM; i++)
    {
        ThreadData *p = new ThreadData;
        p->num = i+1;
        char buffer[64];
        snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i);
        pthread_create(&p->tid, nullptr, pthread_routine, p);
        arr.push_back(p);
        //sleep(1);
    }
    for(auto& e : arr)
    {
        void* ret = nullptr;//注意,这里是void*
        int n = pthread_join(e->tid, &ret);//这里传过去的是ret的地址,等待传回来的返回值是void*的,就等于直接写入的ret当中
        assert(n==0);
        cout << "等待线程:" << e->buffer << "成功"<< "编号:" << (long long)e->num << endl;//因为是64位系统,需要转换成longlong类型 
        delete e;
    }
    while(true)
    {
        cout << "main thread" << endl;
        sleep(1);
    }
    return 0;
}

同理,线程中再堆区开辟的空间地址也可以拿到。

线程取消

注意:线程取消的前提是线程已经在运行了。

参数是线程id。

#include <iostream>
#include <pthread.h>
#include <cassert>
#include <string>
#include <vector>
#include <unistd.h>
#include <cstdlib>
using namespace std;
struct ThreadData
{
    int num;//线程的编号
    pthread_t tid;
    char buffer[64];
};
void *pthread_routine(void* args)
{
    ThreadData* p = static_cast<ThreadData*>(args);
    while(true)
    {
        cout << "new " << p->buffer << endl;
        sleep(1);
    }
    return (void*)100;//正常退出就返回100
}
int main()
{
    vector<ThreadData*> arr;
#define NUM 10
    for(int i = 0; i < NUM; i++)
    {
        ThreadData *p = new ThreadData;
        p->num = i+1;
        char buffer[64];
        snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i);
        pthread_create(&p->tid, nullptr, pthread_routine, p);
        arr.push_back(p);
        //sleep(1);
    }
    sleep(3);
    for(auto& e : arr)
    {
        pthread_cancel(e->tid);
        cout << "取消的线程id:" << e->tid << "成功" << endl;
    }
    for(auto& e : arr)
    {
        void* ret = nullptr;
        int n = pthread_join(e->tid, &ret);
        assert(n==0);
        cout << "等待线程:" << e->buffer << "成功"<< "编号:" << (long long)ret << endl;//因为是64位系统,需要转换成longlong类型 
        delete e;
    }
    while(true)
    {
        cout << "main thread" << endl;
        sleep(1);
    }
    return 0;
}

线程一旦被取消,退出码就是-1。

相关文章
|
9月前
|
存储 Linux API
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
在计算机系统的底层架构中,操作系统肩负着资源管理与任务调度的重任。当我们启动各类应用程序时,其背后复杂的运作机制便悄然展开。程序,作为静态的指令集合,如何在系统中实现动态执行?本文带你一探究竟!
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
|
7月前
|
并行计算 Linux
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
279 67
|
9月前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
通过本文,您可以了解如何在业务线程中注册和处理Linux信号。正确处理信号可以提高程序的健壮性和稳定性。希望这些内容能帮助您更好地理解和应用Linux信号处理机制。
162 26
|
9月前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
本文详细介绍了如何在Linux中通过在业务线程中注册和处理信号。我们讨论了信号的基本概念,并通过完整的代码示例展示了在业务线程中注册和处理信号的方法。通过正确地使用信号处理机制,可以提高程序的健壮性和响应能力。希望本文能帮助您更好地理解和应用Linux信号处理,提高开发效率和代码质量。
170 17
|
算法 Unix Linux
linux线程调度策略
linux线程调度策略
333 0
|
资源调度 Linux 调度
Linux C/C++之线程基础
这篇文章详细介绍了Linux下C/C++线程的基本概念、创建和管理线程的方法,以及线程同步的各种机制,并通过实例代码展示了线程同步技术的应用。
215 0
Linux C/C++之线程基础
|
安全 Linux
Linux线程(十一)线程互斥锁-条件变量详解
Linux线程(十一)线程互斥锁-条件变量详解
|
存储 设计模式 NoSQL
Linux线程详解
Linux线程详解
|
缓存 Linux C语言
Linux线程是如何创建的
【8月更文挑战第5天】线程不是一个完全由内核实现的机制,它是由内核态和用户态合作完成的。
|
负载均衡 Linux 调度
在Linux中,进程和线程有何作用?
在Linux中,进程和线程有何作用?