【Linux】线程概述、创建线程、终止线程

橙色

线程概述

与进程类似,线程是允许应用程序并发执行多个任务的一种机制。一个进程可以包含多个线程。

进程是 CPU 分配资源的最小单位,线程是操作系统调度执行的最小单位。

线程是轻量级进程(LWP:Light Wight Process),在 Linux 环境下线程的本质仍是进程。

查看指定进程的 LWP 号:ps -Lf pid

进程间的信息难以共享,进程通信较难;调用 fork() 来创建进程的代价相对较高。而线程通过共享解决了这些问题。

线程之间的共享和非共享资源

  • 共享资源:进程 ID 和父进程 ID,进程组 ID 和会话 ID,用户 ID 和用户组 ID,文件描述符,信号处理,文件系统的相关信息,虚拟地址空间(除栈和 .text)。
  • 非共享资源:线程 ID,信号掩码,线程特有数据,error 变量,实时调度策略和优先级,栈、本地变量和函数的调用链接信息

查看当前 pthread 库版本:getconf GNU_LIBPTHREAD_VERSION

1、创建线程

函数解析

/*
    一般情况下,main函数所在的线程我们称之为主线程(main线程),其余创建的线程
    称之为子线程。
    程序中默认只有一个进程,fork()函数调用,2进行
    程序中默认只有一个线程,pthread_create()函数调用,2个线程。

    #include <pthread.h>
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 
    void *(*start_routine) (void *), void *arg);

        - 功能:创建一个子线程
        - 参数:
            - thread:传出参数,线程创建成功后,子线程的线程ID被写到该变量中。
            - attr : 设置线程的属性,一般使用默认值,NULL
            - start_routine : 函数指针,这个函数是子线程需要处理的逻辑代码
            - arg : 给第三个参数使用,传参
        - 返回值:
            成功:0
            失败:返回错误号。这个错误号和之前errno不太一样。
            获取错误号的信息:  char * strerror(int errnum);

*/

代码举例

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

void * callback(void * arg) {
    
    
    printf("child thread...\n");
    printf("arg value: %d\n", *(int *)arg);
    return NULL;
}

int main() {
    
    

    pthread_t tid;

    int num = 10;

    // 创建一个子线程
    int ret = pthread_create(&tid, NULL, callback, (void *)&num);

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

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

    sleep(1);

    return 0;   // exit(0);
}

编译运行该程序(注意,编译需要带上它的动态库,不然会报错,可以复习一下前面动态库静态库的内容),结果如下:
在这里插入图片描述
整个main函数中的就是主线程的内容,而callback函数中的就是其他线程的内容,如果还想创建更多线程的话,继续int ret = pthread_create(&tid, NULL, callback, (void *)&num);即可,新线程里的内容可以是callback,也可以自己在创建新的函数传进去。

2、终止线程

函数解析

/*

    #include <pthread.h>
    void pthread_exit(void *retval);
        功能:终止一个线程,在哪个线程中调用,就表示终止哪个线程
        参数:
            retval:需要传递一个指针,作为一个返回值,可以在pthread_join()中获取到。

    pthread_t pthread_self(void);
        功能:获取当前的线程的线程ID

    int pthread_equal(pthread_t t1, pthread_t t2);
        功能:比较两个线程ID是否相等
        不同的操作系统,pthread_t类型的实现不一样,有的是无符号的长整型,有的
        是使用结构体去实现的。所以需要调用该函数,而不是直接用"="来进行比较
*/

代码举例

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

void * callback(void * arg) {
    
    
    //获取当前线程的id
    printf("child thread id : %ld\n", pthread_self());
    return NULL;    // pthread_exit(NULL);
} 

int main() {
    
    

    // 创建一个子线程
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, callback, NULL);

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

    // 主线程
    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);

    printf("main thread exit\n");

    return 0;   // exit(0);
}

可以看到,当pthread_exit(NULL)执行后,主线程就已经退出了,所以后面的printf("main thread exit\n")就没有执行了。在结果中,没有看到打印出main thread exit这句话
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/mhyasadj/article/details/130898568