线程知识点

一、线程

1.定义

线程:是一个进程并发执行多种任务的机制。

串行:多个任务有序执行,一个任务执行完毕后,再去执行下一个任务

并发:多个任务在单个CPU上运行,同一个时间片上只能运行一个任务,cpu不停在各个任务上切换

并行:多任务在多个cpu上运行,同一个时间片上可以执行多个任务

其中上下文:运行一个进程所需要的所有资源

上下文切换:切换进程时,cpu访问的资源需要替换原先的资源,进程的上下文切换是个耗时操作,所以引入线程。

因为线程属于同一进程下,共享其附属进程的所有资源。

2.进程和线程的区别

1.进程时资源分配的最小单位,线程是任务运行的最小单位

2.进程和进程之间相互独立,内核空间共享。进程之间数据通信需要引进IPC通信机制

3.线程与线程之间共享其附属进程的所有资源,所以线程之间通信不需要通信机制,但是需要注意同步互斥

4.多线程的效率比多进程高,多进程的稳定性比多线程高,多进程的资源量比多线程高

二、线程的创建  pthread_creat

gcc时需要加-pthread

#include <head.h>

// 线程执行体,里面写副线程要执行的内容
void *callBack(void *arg) // void *arg=NULL
{
    while (1)
    {
        printf("副线程\n");
        sleep(1);
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
    // 创建一个分支线程
    pthread_t tid;
    if (pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        // 第一个参数:线程成功创建后的tid号
        // 第二个参数:线程属性,一般填NULL,代表默认属性
        // 第三个参数:回调函数,void *(callback)(void *) 函数指针,指向返回值void*类型,参数列表式void*类型的函数
        // 第四个参数:传递给回调函数的参数
        fprintf(stderr, "创建线程失败");
    }
    while (1)
    {
        printf("主线程\n");
        sleep(1);
    }
    return 0;
}

 

pthread_creat传参

i.主线程向子线程传参

#include <head.h>

// 线程执行体,里面写副线程要执行的内容
void *callBack(void *arg) // void *arg=NULL
{
    while (1)
    {
        printf("副线程 a=%d,&a=%p\n", *(int *)arg, arg);
        sleep(1);
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
    int a = 10;
    // 创建一个分支线程
    pthread_t tid;
    if (pthread_create(&tid, NULL, callBack, (void *)&a) != 0)
    {
        fprintf(stderr, "创建线程失败");
    }
    while (1)
    {
        printf("主线程   a=%d,&a=%p\n", a, &a);
        sleep(1);
    }
    return 0;
}

ii.子线程向主线程传参

#include <head.h>

// 线程执行体,里面写副线程要执行的内容
void *callBack(void *arg) // void *arg=&pa   &pa本身是int **类型
{
    int a = 10;
    *(int **)arg = &a; // 二级指针int**类型,解引用后,访问的是int**类型
    // 如果不强转,void*类型,就不知道要访问多少个字节
    while (1)
    {
        printf("副线程 a=%d,&a=%p\n", a, &a);
        sleep(1);
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
    int *pa = NULL;
    // 创建一个分支线程
    pthread_t tid;
    if (pthread_create(&tid, NULL, callBack, (void *)&pa) != 0) // 要修改pa的值就要把pa的地址传过去
    {
        fprintf(stderr, "创建线程失败");
    }
    while (1)
    {
        if (pa != NULL)
        {
            printf("主线程   a=%d,&a=%p\n", *pa, pa);
            sleep(1);
        }
    }
    return 0;
}

三、线程的退出与回收 pthread_exit和pthread_join

i.不接收退出状态值

#include <head.h>

// 线程执行体,里面写副线程要执行的内容
void *callBack(void *arg) // void *arg=NULL
{
    int i = 0;
    while (i < 3)
    {
        printf("分支线程\n");
        sleep(1);
        i++;
    }
    printf("分支线程准备退出\n");
    pthread_exit(NULL); // 用于退出线程,不想传递退出状态值就填NULL
    return NULL;
}

int main(int argc, char const *argv[])
{
    // 创建一个分支线程
    pthread_t tid;
    if (pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        fprintf(stderr, "创建线程失败");
    }
    pthread_join(tid, NULL);//阻塞等待分支进程结束
    // 第一个参数:分支线程的tid号
    // 第二个参数:null为不接收线程退出的状态值
    // 不为null则将pthread_exit传递的退出状态复制到该二级指针指向的一级指针中。
    printf("主线程准备退出\n");
    return 0;
}

ii.接收退出的状态值 

#include <head.h>

// 线程执行体,里面写副线程要执行的内容
void *callBack(void *arg) // void *arg=NULL
{
    int i = 0;
    while (i < 3)
    {
        printf("分支线程\n");
        sleep(1);
        i++;
    }
    printf("分支线程准备退出\n");
    static int a = 10; // 若不加static,则分支进程结束了之后,a也将不存在,加static来延长生命周期
    pthread_exit(&a);
    return NULL;
}

int main(int argc, char const *argv[])
{
    // 创建一个分支线程
    pthread_t tid;
    if (pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        fprintf(stderr, "创建线程失败");
    }
    void *ptr = NULL;
    pthread_join(tid, &ptr);
    printf("%d\n", *(int *)ptr);
    printf("主线程准备退出\n");
    return 0;
}

练习:分别用两个线程来拷贝一张图片的前半部分和后半部分

#include <head.h>

// 拷贝前半部分
void *callback1(void *arg)
{
    // 以读的方式打开文件
    int fd_r = open("./1.png", O_RDONLY);
    if (fd_r < 0)
    {
        perror("open");
        return NULL;
    }
    // 以写的方式打开文件
    int fd_w = open("./copy.png", O_WRONLY);
    if (fd_w < 0)
    {
        perror("open");
        return NULL;
    }
    off_t size = lseek(fd_r, 0, SEEK_END); // 通过文件的偏移量来计算文件的大小
    // lseek的返回值为距离文件开头的偏移量

    // 修改文件偏移量到文件开头位置
    lseek(fd_r, 0, SEEK_SET);
    lseek(fd_w, 0, SEEK_SET);

    char c = 0;
    for (int i = 0; i < size / 2; i++)
    {
        read(fd_r, &c, 1);
        write(fd_w, &c, 1);
    }
    printf("前半部分拷贝完\n");
    // 关闭文件
    close(fd_r);
    close(fd_w);

    pthread_exit(NULL);
}

// 拷贝后半部分
void *callback2(void *arg)
{
    // 以读的方式打开文件
    int fd_r = open("./1.png", O_RDONLY);
    if (fd_r < 0)
    {
        perror("open");
        return NULL;
    }
    // 以写的方式打开文件
    int fd_w = open("./copy.png", O_WRONLY);
    if (fd_w < 0)
    {
        perror("open");
        return NULL;
    }
    off_t size = lseek(fd_r, 0, SEEK_END); // 通过文件的偏移量来计算文件的大小
    // lseek的返回值为距离文件开头的偏移量

    // 修改文件偏移量到文件开头位置
    lseek(fd_r, size / 2, SEEK_SET);
    lseek(fd_w, size / 2, SEEK_SET);

    char c = 0;
    for (int i = size / 2; i < size; i++)
    {
        read(fd_r, &c, 1);
        write(fd_w, &c, 1);
    }
    printf("后半部分拷贝完\n");
    // 关闭文件
    close(fd_r);
    close(fd_w);

    pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
    // 两个线程在拷贝前,确保文件存在且是清空状态
    int fd_w = open("./copy.png", O_WRONLY | O_CREAT | O_TRUNC, 0664);
    if (fd_w < 0)
    {
        perror("open");
        return -1;
    }
    close(fd_w);

    // 创建两个线程
    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, callback1, NULL) != 0)
    {
        fprintf(stderr, "分支线程1创建失败\n");
        return -1;
    }

    if (pthread_create(&tid2, NULL, callback2, NULL) != 0)
    {
        fprintf(stderr, "分支线程1创建失败\n");
        return -1;
    }

    // 阻塞等待分支线程退出
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    return 0;
}

四、分离线程 pthread_detach

分离线程,线程退出后资源由内核自动回收

当使用pthread_detach分离tid后,pthread_join就无法再回首tid线程的资源了且pthread_join不再阻塞

所以pthread_join和pthread_detach挑一个使用。

#include <head.h>

// 线程执行体,里面写副线程要执行的内容
void *callBack(void *arg) // void *arg=NULL
{
    printf("副线程\n");
    pthread_exit(NULL);
    return NULL;
}

int main(int argc, char const *argv[])
{
    // 创建一个分支线程
    pthread_t tid;
    if (pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        fprintf(stderr, "创建线程失败");
    }
    pthread_detach(tid);
    printf("主线程\n");
    return 0;
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值