《Linux 应用编程》—第13章 Linux 多线程编程

1 多线程概述

1.1 什么是线程

线程是进程内的顺序执行流,一个进程中能够并发多条线程,每条线程并行执行不一样的任务。html

1.2 线程与进程的关系

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

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

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

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. 终止线程

  • 进程的终止

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

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

    三、经过进程的某个其余线程调用exit()函数。任何一个线程调用exit()都会致使进程退出

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

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

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

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

    主线程/进程的结束致使全部线程的结束。

    二、在主线程中调用pthread_exit, 则仅仅是主线程结束,进程不会结束,进程内的其余线程也不会结束,直到全部线程结束,进程才会终止。

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

    三、在任何一个线程中调用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!\n",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\n",t);
        rc = pthread_create(&threads[t],NULL,PrintHello,(void*)t);
        if(rc)
        {
            printf("ERROR;return code from pthread_create() is %d\n",rc);
            exit(-1);
        }
    }
    printf("In main:exit!\n");
    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...\n",tid);
    for(i=0;i<1000000;i++)
    {
        result = result + sin(i)*tan(i);
    }
    printf("Thread %ld done.Result =%e\n",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\n", t);
        rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t);
        if(rc)
        {
            printf("ERROR; return code from pthread_create() is %d\n", 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\n", rc);
            exit(-1);
        }
        printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
    }
    printf("Main: program completed. Exiting.\n");
    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\n",tinfo->thread_num,&p,tinfo->argv_string);
    uargv = strdup(tinfo->argv_string);
    if(uargv == NULL)
        handle_error("strdup");

    for(p = uargv;*p!='\0';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...\n",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\n",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 条件变量

待总结