3.1~3.7 线程概述

简介: 3.1~3.7 线程概述

01 线程概述



并发:宏观上同时,微观上交替


一个进程可以包含多个线程


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


线程是轻量级的进程LWP


线程间共享信息方便


02 线程和进程的区别



fork创建进程的代价相对高,开销大


线程之间能够更方便快速地共享信息


创建线程容易,共享内存,速度快


03 线程和进程虚拟地址空间



04 线程之前共享和非共享资源



05 NPTL



查看pthread库版本



06 线程操作



pthread_self()获取当前线程ID


pthread_create


/*
    一般情况下,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);  //不加子线程可能抢占不到CPU,不执行
    return 0;   // exit(0);
}


pthread_exit


/*
    #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) {
    printf("child thread id : %ld\n", pthread_self());  //pthread_self()获取当前线程的ID
    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_join


/*
    #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());
    // sleep(3);
    // return NULL; 
    // int value = 10; // 局部变量
    pthread_exit((void *)&value);   // return (void *)&value;
} 
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_join()回收子线程的资源
    int * thread_retval;
    ret = pthread_join(tid, (void **)&thread_retval);  //回收子线程tid
    if(ret != 0) {  //回收失败的话   0表示成功
        char * errstr = strerror(ret);
        printf("error : %s\n", errstr);
    }
    printf("exit data : %d\n", *thread_retval);
    printf("回收子线程资源成功!\n");
    // 让主线程退出,当主线程退出时,不会影响其他正常运行的线程。
    pthread_exit(NULL);
    return 0; 
}



pthread_detach


/*
    #include <pthread.h>
    int pthread_detach(pthread_t thread);
        - 功能:分离一个线程。被分离的线程在终止的时候,会自动释放资源返回给系统。
          1.不能多次分离,会产生不可预料的行为。
          2.不能去连接一个已经分离的线程,会报错。
        - 参数:需要分离的线程的ID
        - 返回值:
            成功:0
            失败:返回错误号
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
    printf("chid thread id : %ld\n", pthread_self());
    return NULL;
}
int main() {
    // 创建一个子线程
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, callback, NULL);
    if(ret != 0) {
        char * errstr = strerror(ret);
        printf("error1 : %s\n", errstr);
    }
    // 输出主线程和子线程的id
    printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
    // 设置子线程分离,子线程分离后,子线程结束时对应的资源就不需要主线程释放
    ret = pthread_detach(tid);
    if(ret != 0) {
        char * errstr = strerror(ret);
        printf("error2 : %s\n", errstr);
    }
    // 设置分离后,对分离的子线程进行连接 pthread_join()
    /*
    ret = pthread_join(tid, NULL);
    if(ret != 0) {
        char * errstr = strerror(ret);
        printf("error3 : %s\n", errstr);
    }
    */
    pthread_exit(NULL);
    return 0;
}



pthread_cancel


 /*
    #include <pthread.h>
    int pthread_cancel(pthread_t thread);
        - 功能:取消线程(让线程终止)
            取消某个线程,可以终止某个线程的运行,
            但是并不是立马终止,而是当子线程执行到一个取消点,线程才会终止。
            取消点:系统规定好的一些系统调用,我们可以粗略的理解为从用户区到内核区的切换,这个位置称之为取消点。
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
    printf("chid thread id : %ld\n", pthread_self());
    for(int i = 0; i < 10; i++) {
        printf("child : %d\n", i);
    }
    return NULL;
}
int main() {
    // 创建一个子线程
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, callback, NULL);
    if(ret != 0) {
        char * errstr = strerror(ret);
        printf("error1 : %s\n", errstr);
    }
    // 取消子线程
    pthread_cancel(tid); 
    for(int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
    // 输出主线程和子线程的id
    printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
    pthread_exit(NULL);
    return 0;
}



07 线程属性



/*
    int pthread_attr_init(pthread_attr_t *attr);
        - 初始化线程属性变量
    int pthread_attr_destroy(pthread_attr_t *attr);
        - 释放线程属性的资源
    int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
        - 获取线程分离的状态属性
    int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
        - 设置线程分离的状态属性
*/     
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
    printf("chid thread id : %ld\n", pthread_self());
    return NULL;
}
int main() {
    // 创建一个线程属性变量
    pthread_attr_t attr;
    // 初始化属性变量
    pthread_attr_init(&attr);
    // 设置属性
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    // 创建一个子线程
    pthread_t tid;
    int ret = pthread_create(&tid, &attr, callback, NULL);   //用设置好的线程属性创建线程
    if(ret != 0) {
        char * errstr = strerror(ret);
        printf("error1 : %s\n", errstr);
    }
    // 获取线程的栈的大小
    size_t size;
    pthread_attr_getstacksize(&attr, &size);  //获取到的大小保存到size
    printf("thread stack size : %ld\n", size);
    // 输出主线程和子线程的id
    printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
    // 释放线程属性资源
    pthread_attr_destroy(&attr);
    pthread_exit(NULL);
    return 0;
}


相关文章
|
22天前
|
Java
[并发编程基础] Java线程的创建方式
[并发编程基础] Java线程的创建方式
|
22天前
|
缓存 C语言
线程是如何创建的
【2月更文挑战第14天】
|
22天前
|
Java API 调度
Java多线程基础(线程与进程的区别,线程的创建方式及常用api,线程的状态)
Java多线程基础(线程与进程的区别,线程的创建方式及常用api,线程的状态)
60 0
Java多线程基础(线程与进程的区别,线程的创建方式及常用api,线程的状态)
|
存储 缓存 资源调度
多线程的概述
多线程的概述
java线程的三种创建方式详细分析(全)
目录前言1. 继承Thread类2. 实现Runnable 接口3. 通过Callable和Future创建线程4. 总结 前言 关于线程这部分内容讲的比较多,可看我之前的文章 【操作系统】线程与进程的深入剖析(全) 【操作系统】守护线程和守护进程的区别 对于线程Thread的分析 也可看我之前的文章 java之Thread类详细分析(全) java之Thread类实战模板(全) 多线程中run()和start()的异同详细分析(全) 对于创建方式的汇总以及方式 可看我这篇文章的总结对比 1. 继承
java线程的三种创建方式详细分析(全)
|
安全 Java 调度
多线程概述
一.线程相关概念 (1)程序 是为了完成特定任务、用某种语言编写的一组指令的集合。简单的说就是我们写的代码。 (2)进程 ①进程是指一个内存中运行的应用程序,每个进程都有一个独立的内存空间,一个应用程序可以同时运行多个进程; ②进程也是程序的一次执行过程,是系统运行程序的基本单位;系统运行一个程序即是一个进程从创建、运行到消亡的过程。 比如我们使用的QQ,就启动了一个进程,操作系统就会为该进程分配内存空间。当我们使用迅雷,又启动了一个进程,操作系统将为迅雷分配新的内存空间。
多线程概述
|
设计模式 监控 安全
Java多线程(完整版)、基本概念:程序、进程、线程、线程的创建和使用、线程的生命周期、线程的同步、线程的通信、JDK5.0新增线程创建方式、wait(),notify(),notifyAll()
Java多线程(完整版)、基本概念:程序、进程、线程、线程的创建和使用、线程的生命周期、线程的同步、线程的通信、JDK5.0新增线程创建方式、wait(),notify(),notifyAll()
Java多线程(完整版)、基本概念:程序、进程、线程、线程的创建和使用、线程的生命周期、线程的同步、线程的通信、JDK5.0新增线程创建方式、wait(),notify(),notifyAll()
|
Java 调度
多线程基本概念(并发与并行、线程与进程)和入门案例
多线程基本概念(并发与并行、线程与进程)和入门案例
【多线程学习笔记1】线程概述
【多线程学习笔记1】线程概述