美文网首页
linux线程的创建、退出、等待、取消、分离

linux线程的创建、退出、等待、取消、分离

作者: FakeCSer爱去网吧 | 来源:发表于2020-02-27 10:45 被阅读0次

进程与线程

  • 进程是计算机拥有资源的基本单位;线程是计算机调度的基本单位,是CPU分配时间片的基本单元
  • 一个进程可以拥有>=1个线程,同一进程的多个线程共享地址空间,文件描述符,每种信号的处理方式,当前工作目录,用户ID和组ID。每个线程私有线程号,寄存器(程序计数器,栈指针),堆栈,信号屏蔽字,调度优先级,线程私有的存储空间。
  • 线程引入原因:
    • 创建属于同一进程的另一线程比创建另一个进程花费小很多。
    • 同一进程内线程切换速度快。
    • 线程通信更加简单,不需要通过操作系统。

API

线程创建
#include <pthread.h>
int pthread_create(pthread_t * thread,const pthread_attr_t * attr,
                    void *(*start_routine)(void *),void * arg);
参数 意义
thread 指向线程标识符的指针,用来存储线程标识符
attr 设置线程属性,主要设置与栈相关的属性,一般情况下该参数设置为NULL,新的线程将使用系统默认的属性
start_routine 线程运行函数的起始地址,即在此线程中运行哪些代码
arg 运行函数的参数地址

返回值:成功:0,错误:出错编号。
pthread不是Linux系统默认的库而是POSIX线程库。在Linux中将其作为一个库来使用,因此编译时需要加上-pthread以显式链接该库

获取线程当前ID
include <pthread.h>
pthread_t pthread_self(void);

返回线程ID
线程标识符在进程中是唯一的,即分别属于两不同进程的两个线程可能有相同的线程标识符

#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;

void * thr_fun(void * arg)
{

    cout << "Im thread" << endl;
    return((void*)0);
}

int main()
{

    int ret;
    pthread_t ntid;
    
    ret = pthread_create(&ntid,NULL,thr_fun,NULL);
    if(ret != 0)
    {
        perror("pthreadcreate");
        exit(1);
    }
    sleep(2);   //attention
    exit(0);
}
线程退出与等待
  • 线程退出
    线程主动结束
#include <pthread.h>
void pthread_exit(void * retval);

retval:返回信息

主线程用pthread_exit还是return
用pthread_exit只会使主线程自身退出,产生的子线程继续执行;用return则所有线程退出。

  • 线程等待
#include <pthread.h>
int pthread_join(pthread_t thread,void **retval);

参数表:
thread: 要等待的线程的pid
retval:用来存储被等待线程的返回值
返回0:成功;返回错误号:失败
主线程阻塞自己,等待子线程结束,然后回收子线程资源

示例代码:
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;

void * thr_fun(void * arg)
{

    int i = 5;
    while(i>0)
    {
        sleep(1);
        cout << "thread " <<pthread_self()<<" is running"<< endl;
        i--;
    }
    pthread_exit((void *)"testtest");//返回出错信息
}

int main()
{

    int ret;
    void * thread1_ret;
    pthread_t thread1;
    ret = pthread_create(&thread1,NULL,thr_fun,NULL);
    if(ret != 0)
    {
        perror("pthreadcreate");
        exit(1);
    }
    
    pthread_join(thread1,&thread1_ret);//获取出错信息
    cout <<(char*)thread1_ret << endl;
    exit(0);
}

线程终止
#include <pthread.h>
int pthread_cancel(pthread_t thread);
示例代码
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;

void * thr_fun(void * arg)
{

    cout << "thread " <<pthread_self()<<" will running for 5 seconds"<< endl;
    int i = 5;
    while(i>0)
    {
        sleep(1);
        cout << "thread " <<pthread_self()<<" is running"<< endl;
        i--;
    }
    return((void*)0);
}

int main()
{

    int ret;
    pthread_t thread1;

    ret = pthread_create(&thread1,NULL,thr_fun,NULL);
    if(ret != 0)
    {
        perror("pthreadcreate");
        exit(1);
    }

    sleep(4);
    cout << "cancel thread " << thread1 << "!" << endl;

    ret = pthread_cancel(thread1);
    if(ret != 0)
    {
        perror("pthreadcancel");
        exit(1);
    }
    ret = pthread_join(thread1,NULL);
    if(ret != 0)
    {
        perror("pthreadJOIN");
        exit(1);
    }
    exit(0);
}
取消状态设置

可以设置线程能否被取消和取消后是否立即执行

  • 取消状态设置
#include <pthread.h>
int pthread_setcancelstate(int state,int * oldstate);

参数表
state:PTHREAD_CANCEL_DISABLE或者PTHREAD_CANCEL_ENABLE
oldstate:指针类型,上一次取消状态的指针,可设NULL

  • 取消类型设置(取消是否立即生效)
#include <pthread.h>
int pthread_setcanseltype(int type,int old *type)

type:PTHREAD_CANCEL_ASYNCHRONOUS立即取消
PTHREAD_CANCEL_DEFERRED等待事件(如pthread_join时)才取消

线程分离

在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached)。一个可结合的线程能够被其他线程收回其资源和杀死,只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源;在被其他线程回收之前,它的存储器资源(如栈)是不释放的。相反,一个分离的线程是不能被其他线程回收或杀死的,它的存储器资源在它终止时由系统自动释放。因此为了避免内存泄漏,所有线程的终止,要么已设为DETACHED,要么就需要使用pthread_join()来回收

#include <pthread.h>
int pthread_detach(pthread_t threadID);

返回0成功,错误号失败
分离后不可以再合并。该操作不可逆

综合以上要想让子线程总能完整执行(不会中途退出),

  • 一种方法是在主线程中调用pthread_join对其等待,即pthread_create/pthread_join/pthread_exit或return;
  • 一种方法是在主线程退出时使用pthread_exit,这样子线程能继续执行,即pthread_create/pthread_detach/pthread_exit;
  • 还有一种是pthread_create/pthread_detach/return,这时就要保证主线程不能退出,至少是子线程完成前不能退出。

注:很多地方参照了黄茹老师主编的《Linux环境高级程序设计》

相关文章

网友评论

      本文标题:linux线程的创建、退出、等待、取消、分离

      本文链接:https://www.haomeiwen.com/subject/gmazchtx.html