zoukankan      html  css  js  c++  java
  • 《Linux 应用编程》—第13章 Linux 多线程编程

    1 多线程概述

    1.1 什么是线程

    线程是进程内的顺序执行流,一个进程中可以并发多条线程,每条线程并行执行不同的任务。

    1.2 线程与进程的关系

    • 一个线程只能属于一个进程,一个进程可以包含多个线程,但是至少有一个主线程
    • 资源分配给进程,同一进程的所有线程共享该进程的所有资源
    • 线程作为调度和分配的基本单位,进程作为拥有资源的基本单位(这里说的是操作系统资源吧)
    • 在创建或撤销进程时,由于系统都要为之分配和回收资源,导致系统的开销大于创建或撤销线程时的开销(也就是说创建或撤销进程包含创建或撤销线程,同时还有分配和回收资源)

    小声bb:在FreeRTOS等小型操作系统,或者说是任务调度微内核里面好像没有进程的概念.

    引用"阮一峰"的博客内容

    CPU:工厂; 假定工厂的电力有限,一次只能供给一个车间使用。也就是说,一个车间开工的时候,其他车间都必须停工。背后的含义就是,单个CPU一次只能运行一个任务。

    进程:车间; 进程就好比工厂的车间,它代表CPU所能处理的单个任务。任一时刻,CPU总是运行一个进程,其他进程处于非运行状态。

    一个车间里,可以有很多工人。他们协同完成一个任务。

    线程:工人; 线程就好比车间里的工人。一个进程可以包括多个线程。

    车间的空间是工人们共享的,比如许多房间(内存空间)是每个工人都可以进出的。这象征一个进程的内存空间是共享的,每个线程都可以使用这些共享内存

    可是,每间房间的大小不同,有些房间最多只能容纳一个人,比如厕所。里面有人的时候,其他人就不能进去了。这代表一个线程使用某些共享内存时,其他线程必须等它结束,才能使用这一块内存。

    一个防止他人进入的简单方法,就是门口加一把锁。先到的人锁上门,后到的人看到上锁,就在门口排队,等锁打开再进去。这就叫"互斥锁"(Mutual exclusion,缩写 Mutex),防止多个线程同时读写某一块内存区域。

    还有些房间,可以同时容纳n个人,比如厨房。也就是说,如果人数大于n,多出来的人只能在外面等着。这好比某些内存区域,只能供给固定数目的线程使用。

    时的解决方法,就是在门口挂n把钥匙。进去的人就取一把钥匙,出来时再把钥匙挂回原处。后到的人发现钥匙架空了,就知道必须在门口排队等着了。这种做法叫做"信号量"(Semaphore),用来保证多个线程不会互相冲突。

    不难看出,mutex是semaphore的一种特殊情况(n=1时)。也就是说,完全可以用后者替代前者。但是,因为mutex较为简单,且效率高,所以在必须保证资源独占的情况下,还是采用这种设计。

    1.3 为什么使用多线程

    • 方便通信和数据交换

      线程间有方便的通信和数据交换机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。

    • 更高效的利用CPU

      使用多线程可以提高应用程序响应(说明多线程也是轮转的)。这对图形界面的程序尤其有意义,当一个操作耗时很长时,整个系统都会等待这个操作,此时程序不会响应键盘、鼠标、菜单的操作,而使用多线程技术,将耗时长的操作置于一个新的线程,可以避免这种尴尬的情况。

    2 POSIX Threads 概述

    POSIX Threads(通常简称为 Pthreads)定义了创建和操纵线程的一套 API 接口, 一般用于 Unix-like POSIX 系统中(如 FreeBSD、 GNU/Linux、 OpenBSD、 Mac OS 等系统)。

    Pthreads接口根据功能划分:

    • 线程管理
    • 互斥量
    • 条件变量
    • 同步

    写Pthreads多线程程序的时远源码需要包含pthread.h头文件,LDFLAGS += -pthread,可以用来指定需要包含的库。Makefile 选项 CFLAGS 、LDFLAGS 、LIBS可以了解一下。

    3 线程管理

    3.1 线程ID

    定义:可以看做是线程的句柄,用来引用一个线程

    • pthread_self函数

      • 作用:获取线程自己的ID
      • 返回值:pthread_t
      • 形参:void
    • pthread_equal函数

      • 作用:比较两个线程ID是否相等
      • 返回值:相等返回非0值,否则返回0
      • 形参:pthread_t类型的两个参数

    3.2 创建与终止

    1. 创建线程

    • pthread_create()函数
      • 作用:在进程中创建一个新线程
      • 返回值: pthread_create()调用成功,函数返回 0,否则返回一个非 0 的错误码
      • 形参:thread指向新创建的线程ID、attr为线程属性对象、start_routine是线程开始时调用的的函数的名字、arg为start_routine指定的函数的参数。

    2. 终止线程

    • 进程的终止

      1、直接调用exit()。任何一个线程调用exit()都会导致进程退出

      2、执行main()函数中的return。 进程的主函数终止了进程也就结束了

      3、通过进程的某个其他线程调用exit()函数。任何一个线程调用exit()都会导致进程退出

    • 主线程、子线程调用exit, pthread_exit,互相产生的影响。

      1、在主线程中,在main函数中return了或是调用了exit函数,则主线程退出,且整个进程也会终止,此时进程中的所有线程也将终止。因此要避免main函数过早结束。【隐式调用】

      任何线程调用exit()都会导致进程结束。

      主线程的main函数跑到return语句会导致进程结束。

      主线程/进程的结束导致所有线程的结束。

      2、在主线程中调用pthread_exit, 则仅仅是主线程结束,进程不会结束,进程内的其他线程也不会结束,直到所有线程结束,进程才会终止。

      调用pthread_exit的线程只会结束本线程,主线程调用也只会结束自身,不会结束进程。

      3、在任何一个线程中调用exit函数都会导致进程结束。进程一旦结束,那么进程中的所有线程都将结束。

      进程内任何地方调用exit()都会结束进程。

    • 主线程

      如果主线程在创建了其它线程后没有任务需要处理,那么它应该阻塞等待所有线程都结束为止,或者应该调用pthread_exit(NULL)。

      调用pthread_exit(NULL)可以减少一个线程开销,看一下线程怎么阻塞。

    • pthread_exit()函数

      • 作用:使得调用线程终止
      • 返回值:void
      • 形参:retval 是一个 void 类型的指针。
        需要理解“线程的返回值",线程是有返回值的。

      pthread_exit(void *ptr) 函数使线程退出,并返回一个空指针类型的值。

      pthread_join(pthread_t tid,void **rtval)调用此函数的进程/线程等id为tid的线程返回或被终止,并从它那里获得返回值。

      注意,退出函数返回的是一个空指针类型,接收函数也必须用一个指针来接收。但是函数给出的参数是接收指针的地址,即,接收到的指针值写入给出的地址处的指针变量。

    3. 线程范例1

    #include "pthread.h"
    #include "stdio.h"
    #include "stdlib.h"
    #define NUM_THREADS 5
    
    void *PrintHello(void *threadid)
    {
        long tid;
        tid = (long)threadid;
        printf("Hello World!It's me,thread #%ld!
    ",tid);
        pthread_exit(NULL);
    }
    
    int main(int argc,char* argv[])
    {
        pthread_t threads[NUM_THREADS];
        int rc;
        long t;
    
        for (t=0;t<NUM_THREADS;t++)
        {
            printf("In main:creating thread %ld
    ",t);
            rc = pthread_create(&threads[t],NULL,PrintHello,(void*)t);
            if(rc)
            {
                printf("ERROR;return code from pthread_create() is %d
    ",rc);
                exit(-1);
            }
        }
        printf("In main:exit!
    ");
        pthread_exit(NULL);
        return 0;
    }
    

    编译的时候需要加lpthread的库:

    gcc thread_begin_end.c -lpthread
    

    输出如下:

    In main:creating thread 0 
    In main:creating thread 1
    Hello World!It's me,thread #0!
    In main:creating thread 2
    Hello World!It's me,thread #1!
    In main:creating thread 3
    Hello World!It's me,thread #2!
    In main:creating thread 4
    Hello World!It's me,thread #3!
    In main:exit!
    Hello World!It's me,thread #4!
    

    进程内的线程是共享资源的,In main:creating thread In main:exit!是主线程打印出来的;

    Hello World!It's me,thread #好像是子线程打印的,需要看一下pthread_create的形参,形参3是线程开始时候调用的函数,所以就是子线程打印的。

    3.3 连接与分离

    线程可以分为分离线程(DETACHED)非分离线程(JOINABLE)两种。

    • 分离线程是指线程退出时线程将释放它的资源的线程;分离线程退出时不会报告线程状态
    • 非分离线程退出后不会立即释放资源,需要另一个线程为它调用 pthread_join 函数或者进程退出时才会释放资源。

    1. 线程分离

    • pthread_detach()函数 int pthread_detach(pthread_t thread);
      • 作用:可以将非分离线程设置为分离线程
      • 形参:thread 是要分离的线程的 ID。
      • 返回值:成功返回 0;失败返回一个非 0 的错误码。

    2. 线程连接

    • pthread_join()函数 int pthread_join(pthread_t thread, void **retval);
      • 作用:将调用线程挂起,直到第一个参数 thread 指定目标线程终止运行为止。
      • 形参:retval 为指向线程的返回值的指针提供一个位置, 这个返回值是目标线程调用pthread_exit()或者 return 所提供的值。
      • 返回值:成功返回 0;失败返回一个非 0 的错误码。

    3. 线程范例2

    #include "pthread.h"
    #include "stdio.h"
    #include "stdlib.h"
    #include "math.h"
    
    #define NUM_THREADS 4
    
    void *BusyWork(void* t)
    {
        int i;
        long tid;
        double result=0.0;
        tid = (long)t;
    
        printf("Thread %ld starting...
    ",tid);
        for(i=0;i<1000000;i++)
        {
            result = result + sin(i)*tan(i);
        }
        printf("Thread %ld done.Result =%e
    ",tid,result);
        pthread_exit((void*)t);
    }
    
    int main(int argc,char* argv[])
    {
        pthread_t thread[NUM_THREADS];
        int rc;
        long t;
        void *status;
    
        for(t=0;t<NUM_THREADS;t++)
        {
            printf("Main: creating thread %ld
    ", t);
            rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t);
            if(rc)
            {
                printf("ERROR; return code from pthread_create() is %d
    ", rc);
                exit(-1);
            }
        }
        for(t=0;t<NUM_THREADS;t++)
        {
            rc = pthread_join(thread[t], &status);
            if(rc)
            {
                printf("ERROR; return code from pthread_join() is %d
    ", rc);
                exit(-1);
            }
            printf("Main: completed join with thread %ld having a status of %ld
    ",t,(long)status);
        }
        printf("Main: program completed. Exiting.
    ");
        pthread_exit(NULL); 
    }
    

    编译需要是用:

    gcc thread_join.c  -lpthread -lm
    

    输出如下:

    Main: creating thread 0
    Main: creating thread 1
    Thread 0 starting...
    Main: creating thread 2
    Thread 1 starting...
    Main: creating thread 3
    Thread 2 starting...
    Thread 3 starting...
    Thread 0 done.Result =-3.153838e+06
    Thread 3 done.Result =-3.153838e+06
    Thread 1 done.Result =-3.153838e+06
    Main: completed join with thread 0 having a status of 0
    Main: completed join with thread 1 having a status of 1
    Thread 2 done.Result =-3.153838e+06
    Main: completed join with thread 2 having a status of 2
    Main: completed join with thread 3 having a status of 3
    Main: program completed. Exiting.
    

    Main: creating thread是主线程打印出来的,子线程初始化执行的都是BusyWork函数内的内容。

    主线程循环调用pthread_join函数,应该是将主线程挂起,执行到4遍for循环里面的第一个pthread_join函数之后就挂起,等待第一个创建的子线程结束。

    Thread x done.Result =打印没什么特别的,线程的调度是随机的,不确定哪个先结束,随意没有先后顺序

    Main: completed join with thread x having a status of打印有两个特征,一个是顺序打印,原因是在主线程里顺序执行,另一个就是必须在对应的子线程的后面

    挂起阻塞的区别:

    理解一:挂起是一种主动行为,因此恢复也应该要主动完成,而阻塞则是一种被动行为,是在等待事件或资源时任务的表现,你不知道他什么时候被阻塞(pend),也就不能确切 的知道他什么时候恢复阻塞。而且挂起队列在操作系统里可以看成一个,而阻塞队列则是不同的事件或资源(如信号量)就有自己的队列。

    理解二:阻塞(pend)就是任务释放CPU,其他任务可以运行,一般在等待某种资源或信号量的时候出现。挂起(suspend)不释放CPU,如果任务优先级高就永远轮不到其他任务运行,一般挂起用于程序调试中的条件中断,当出现某个条件的情况下挂起,然后进行单步调试。

    理解三:pend是task主动去等一个事件,或消息.suspend是直接悬挂task,以后这个task和你没任何关系,任何task间的通信或者同步都和这个suspended task没任何关系了,除非你resume task;

    理解四:任务调度是操作系统来实现的,任务调度时,直接忽略挂起状态的任务,但是会顾及处于pend下的任务,当pend下的任务等待的资源就绪后,就可以转为ready了。ready只需要等待CPU时间,当然,任务调度也占用开销,但是不大,可以忽略。可以这样理解,只要是挂起状态,操作系统就不在管理这个任务了。

    理解五:挂起是主动的,一般需要用挂起函数进行操作,若没有resume的动作,则此任务一直不会ready。而阻塞是因为资源被其他任务抢占而处于休眠态。两者的表现方式都是从就绪态里“清掉”,即对应标志位清零,只不过实现方式不一样。

    3.4 线程属性

    线程基本属性包括: 栈大小、 调度策略和线程状态。

    属性对象

    • 初始化属性对象

      int pthread_attr_init(pthread_attr_t *attr);

    • 销毁属性对象

      int pthread_attr_destroy(pthread_attr_t *attr);

    线程状态

    • 两种线程状态

      • PTHREAD_CREATE_JOINABLE——非分离线程
      • PTHREAD_CREATE_DETACHED——分离线程
    • 获取线程状态

      int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);

    • 设置线程状态

      int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);

    线程栈

    Linux系统线程的默认栈大小为 8MB,只有主线程的栈大小会在运行过程中自动增长。

    • 获取线程栈

      int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);

    • 设置线程栈

      intpthread_attr_setstacksize(pthread_attr_t *attr, size_tstacksize);

    线程范例3

    #include "pthread.h"
    #include "string.h"
    #include "stdio.h"
    #include "stdlib.h"
    #include "unistd.h"
    #include "errno.h"
    #include "ctype.h"
    
    #define handle_error_en(en,msg)
        do{errno=en;perror(msg);exit(EXIT_FAILURE);}while(0)
    
    #define handle_error(msg)
        do{perror(msg);exit(EXIT_FAILURE);}while(0)
    //宏定义里面为什么要使用 do while(0)?因为可以保证被替换后实现想要的功能
    struct thread_info
    {
        pthread_t   thread_id;
        int         thread_num;
        char        *argv_string;
    };
    
    static void* thread_start(void *arg)//这块void*表明函数返回的是一个指针
    {
        struct thread_info *tinfo  = arg;
        char *uargv,*p;
    
        printf("Thread %d:top of stack near %p;argv_thing =%s
    ",tinfo->thread_num,&p,tinfo->argv_string);
        uargv = strdup(tinfo->argv_string);
        if(uargv == NULL)
            handle_error("strdup");
    
        for(p = uargv;*p!='';p++)
            *p = toupper(*p);
    
        return uargv;
    }
    
    int main(int argc,char *argv[])
    {
        int s,tnum,opt,num_threads;
        struct thread_info *tinfo;
        pthread_attr_t attr;
        int stack_size;
        void *res;
    
        stack_size = -1;
        while((opt = getopt(argc,argv,"s:")) != -1)
        {
            switch(opt)
            {
                case 's':
                    stack_size = strtoul(optarg,NULL,0);
                    break;
                default:
                    fprintf(stderr,"Usage:%s[-s stack-size] arg...
    ",argv[0]);
                    exit(EXIT_FAILURE);
            }
        }
    
        num_threads = argc - optind;//optind哪来的?好像是库里面的,没用extern
    
        s = pthread_attr_init(&attr);
        if(s != 0)
            handle_error_en(s,"pthread_attr_init");
        if(stack_size > 0)
        {
            s = pthread_attr_setstacksize(&attr,stack_size);
            
            if(s != 0)
                handle_error_en(s,"pthread_attr_aetstacksize");
        }
        tinfo = calloc(num_threads,sizeof(struct thread_info));
        if(tinfo == NULL)
            handle_error("calloc");
        for(tnum = 0;tnum < num_threads;tnum++)
        {
            tinfo[tnum].thread_num = tnum +1;
            tinfo[tnum].argv_string = argv[optind + tnum];
            s = pthread_create(&tinfo[tnum].thread_id,&attr,&thread_start,&tinfo[tnum]);
            if(s!=0)
                handle_error_en(s,"pthread_create");
        }
        s = pthread_attr_destroy(&attr);
        if(s!=0)
            handle_error_en(s,"pthread_attr_destory");
        
        for(tnum = 0;tnum < num_threads;tnum++)
        {
            s = pthread_join(tinfo[tnum].thread_id,&res);
            if(s!=0)
                handle_error_en(s,"pthread_join");
            printf("Joined with thread %d;returned value was %s
    ",tinfo[tnum].thread_num,(char*)res);
            free(res);//free函数
        }
        free(tinfo);
        exit(EXIT_SUCCESS);
    }
    

    编译:

    gcc thread_attr.c -lpthread
    

    执行:

    ./a.out -s 0x100000 hola salut servus
    

    -s参数需要结合getopt函数进行理解

    结果:

    Thread 1:top of stack near 0x7fcad561fed0;argv_thing =hola
    Thread 3:top of stack near 0x7fcad4cbfed0;argv_thing =servus
    Thread 2:top of stack near 0x7fcad4dcfed0;argv_thing =salut
    Joined with thread 1;returned value was HOLA
    Joined with thread 2;returned value was SALUT
    Joined with thread 3;returned value was SERVUS
    

    Tips

    getopt函数:

    使用man 3 getopt可以获取到关于getopt函数的相关信息

    用来解析命令的参数,在unidtd.h文件里面被包含,

    需要结合optarg、optind等变量使用。

    宏定义使用do while(0),可以保证被替换后实现想要的功能

    #define handle_error(msg)
       do{perror(msg);exit(EXIT_FAILURE);}while(0)
    

    4 线程安全

    待总结

    5 互斥量

    待总结

    6 条件变量

    待总结

  • 相关阅读:
    性能测试时如何确认并发用户数
    web测试误区:浏览器后退键退出系统会话失效
    读书笔记(一)
    Loadrunner参数化数据配置与更新方式
    常见软件测试类型及特点
    Loadrunner录制脚本与编写脚本的区别
    软件测试常见文档要点及区别
    APP测试之Monkey测试
    Python操作Redis大全
    【IntelliJ IDEA】在idea上操作 git分支合并【如何将远程swagger分支 合并到 远程 master分支上】【如何切换 本地分支】
  • 原文地址:https://www.cnblogs.com/dluff/p/12355325.html
Copyright © 2011-2022 走看看