三、【C++项目】WebServer-多线程开发-详细笔记

目录 

 3.1线程

01/线程概述

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

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

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

 线程操作

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); //创建线程
pthread_t pthread_self(void); //获取线程号ID
int pthread_equal(pthread_t t1, pthread_t t2);//比较两个线程是否相等
void pthread_exit(void *retval); //终止一个线程,在哪个线程中调用,就表示终止哪个线程
int pthread_join(pthread_t thread, void **retval); //和一个已经终止的线程进行连接(回收资源)
int pthread_detach(pthread_t thread); //分离一个线程;被分离的线程在终止的时候,会自动释放资源返回给系统。
int pthread_cancel(pthread_t thread); //取消线程(让线程终止)

3.2创建线程

一般情况,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);
}

执行 gcc pthread_create.c -o create

报错,

 原因:这个线程pthread_create是第三方的库,不是标准库,需要指定库的名称,用 -l

gcc pthread_create.c -o create -l pthread

3.3终止线程

#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());
    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);
}

3.4 连接已终止多线程 

#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);
    if(ret != 0) {
        char * errstr = strerror(ret);
        printf("error : %s\n", errstr);
    }
    printf("exit data : %d\n", *thread_retval);
    printf("回收子线程资源成功!\n");
    // 让主线程退出,当主线程退出时,不会影响其他正常运行的线程。
    pthread_exit(NULL);
    return 0; 
}

3.5 线程的分离

#include <pthread.h>
int pthread_detach(pthread_t thread);
/*
        - 功能:分离一个线程。被分离的线程在终止的时候,会自动释放资源返回给系统。
          1.不能多次分离,会产生不可预料的行为。
          2.不能去连接一个已经分离的线程,会报错。
        - 参数:需要分离的线程的ID
        - 返回值:
            成功:0
            失败:返回错误号
*/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值