• Linux 多线程


    前面博客中的进程是程序执行和资源分配的基本单位。每个进程有自己的储存空间

    而线程是进程内的基本调度单位,同一个进程内的所有线程共享一个进程的资源。

    父子进程中并不共享内存,所以只能通过IPC的方式进行进程之间的信息交换

    线程可以共享进程中的资源,但是要注意避免多个线程同时修改同一份信息,所以需要同步。

    有线程的代码再编译的时候要手动链接库,在gcc 后面加上 -lpthread

    线程的头文件 <pthread.h>

    线程ID

    就像进程一样,每个线程有自己的ID,通过这个函数获取调用者的ID

           #include <pthread.h>
    
           pthread_t pthread_self(void);

    返回值即为线程的ID,该函数不会失败。

    返回值类型pthread_t 被定义为一个 unsigned long 所以打印要用%u,但其他实现中可能是一个结构体类型

    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char const *argv[])
    {
        pthread_t id;
        id = pthread_self();
        printf("%lu
    ",id);
        return 0;
    }

    运行结果

    这ID很长 

    可以使用下面函数来对比两个线程ID是否相同

           #include <pthread.h>
    
           int pthread_equal(pthread_t t1, pthread_t t2);

    返回值为0表示两个ID不相等。

    线程的创建

           #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        前面那个函数的参数

    ptyread_create() 线程被创建后立即执行。

    返回值:成功返回0,失败返回非0的错误码

    终止线程

           #include <pthread.h>
    
           void pthread_exit(void *retval);

    retval  可以用来传返回值

    上面的是自己调用终止自己的线程,还有一个能杀死别人的线程的函数

           int pthread_cancel(pthread_t thread);

    这个容易出现意外,一般情况不使用。

    试一下这两个函数

    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    void firstPthread(void)
    {
        for (int i = 0; i < 10; ++i)
        {
            printf("firstPthread : %d
    ",i);
            sleep(1);
        }
        pthread_exit(NULL);
    }
    
    void secondPthread(void)
    {
        for (int i = 0; i < 10; ++i)
        {
            printf("secondPthread : %d
    ",i);
            sleep(1);
        }
        pthread_exit(NULL);
    }
    
    int main(int argc, char const *argv[])
    {
        pthread_t id,id1,id2;
        int ret;
        id = pthread_self();
        printf("%lu
    ",id);
    
        ret = pthread_create(&id1,NULL,(void*)firstPthread,NULL);
        if(ret != 0)
        {
            printf("firstPthread failed");
        }
    
        ret = pthread_create(&id2,NULL,(void*)secondPthread,NULL);
        if(ret != 0)
        {
            printf("secondPthread failed");    
        }
    
    
        return 0;
    }

    我运行了无数次,结果都不理想

     为什么?

    仔细检查了所有代码。没有任何问题。

    那么为什么连一句话都打不出来呢?甚至都不报错?

    看下面的图

     在主函数中有一个主线程在执行

    当执行到firstPthread() 时,开启第一条子线程,

    子线程执行的同时,主线程仍然继续执行,又碰到了第二个创建线程 secondPthread()

    遂又开启了第二条线程函数,此时 主线程,子线程1,子线程2同时在运行。

    然鹅,这个时候,主线程运行到了return 0 ;  主线程的生命即将终结。

    可是两个子线程还没有运行完,但是主线程不管这么多,他要执行他的代码。

    而当主线程结束的时候,一切都结束了。

    所以我们就能看到上面测试过程中,有的时候能看的到子线程1或者子线程2执行了1次 2次。

    他们抢在主线程死亡前。。发出了生命最后的呐喊,然后随着主线程一起香消玉殒。。。

    既然知道了这个原理,我们就可以解决这个问题了,可以在主线程加一个延时函数,等到子线程结束,主线程再结束

    int main(int argc, char const *argv[])
    {
        pthread_t id,id1,id2;
        int ret;
        id = pthread_self();
        printf("%lu
    ",id);
    
        ret = pthread_create(&id1,NULL,(void*)firstPthread,NULL);
        if(ret != 0)
        {
            printf("firstPthread failed");
        }
    
        ret = pthread_create(&id2,NULL,(void*)secondPthread,NULL);
        if(ret != 0)
        {
            printf("secondPthread failed");    
        }
    
        sleep(20);
    
        return 0;
    }

    果然,这样程序就可以正常执行到主线程结束了

    更多时候我们加一个线程等待函数

           #include <pthread.h>
    
           int pthread_join(pthread_t thread, void **retval);

    thread  要等待的线程ID

    retval   二级指针,用来储存被等待线程的返回值

    创建线程时中有个参数

    attr  如果填写NULL,则写入默认属性,默认是非分离属性。(分离:线程结束时资源会自动释放)

        而NULL创建线程资源不会自动释放,所以需要手动调用用函数pthread_join(),来进行回收资源+等待的作用

    可以调用这个函数将 非分离 的线程转化为 分离 线程

           int pthread_detach(pthread_t thread);

    传入要设置的线程的ID,返回值为0成功,非0失败。

    加入后主函数代码如下:

    int main(int argc, char const *argv[])
    {
        pthread_t id,id1,id2;
        int ret;
        id = pthread_self();
        printf("%lu
    ",id);
    
        ret = pthread_create(&id1,NULL,(void*)firstPthread,NULL);
        if(ret != 0)
        {
            printf("firstPthread failed");
        }
    
        ret = pthread_create(&id2,NULL,(void*)secondPthread,NULL);
        if(ret != 0)
        {
            printf("secondPthread failed");    
        }
    
        pthread_join(id1,NULL); 
        pthread_join(id2,NULL);
    
        return 0;
    }

    接下来我们试一下传参和返回值

    还是原来得代码,我把有改动的地方标记出来

    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    void firstPthread(void *arg)
    {
        int *val = (int *)arg;//定义一个int型指针,接收传入的参数
        int *sum = (int *)malloc(sizeof(int *));//在堆区申请一块空间用来保存返回值
      //注意这里不能在栈上申请,因为局部变量在函数结束的时候被释放,到时候我们的返回值就消失了

      //下面使用刚才传入的参数作为循环次数,同时对返回值进行累加
    for (int i = 0; i < *val; ++i) { printf("firstPthread : %d ",i); sleep(1); sum[0]++; } printf("In firstPthread sum is :%d ", sum[0]);
      //传出函数的返回值 pthread_exit(sum); }
    void secondPthread(void) { for (int i = 0; i < 4; ++i) { printf("secondPthread : %d ",i); sleep(1); } pthread_exit(NULL); } int main(int argc, char const *argv[]) { pthread_t id,id1,id2; int ret;
      //在主函数中定义要传入的参数和返回值,因为返回值用二级指针承接,所以将其定义为void指针。
    int val = 6; void *returnFirstPthread; id = pthread_self(); printf("%lu ",id);
      //传入参数(的地址) ret
    = pthread_create(&id1,NULL,(void*)firstPthread,&val); if(ret != 0) { printf("firstPthread failed"); } ret = pthread_create(&id2,NULL,(void*)secondPthread,NULL); if(ret != 0) { printf("secondPthread failed"); }   //使用指针的地址作为接收参数 pthread_join(id1,&returnFirstPthread); pthread_join(id2,NULL);    printf("firstPthread return value :%d ", *(int *)returnFirstPthread); return 0; }

    运行结果如下:

     可以看出来我们的参数传递进去,返回值也成功传出来了。

     我又写了一个试验,让三个进程同时操作一个常量,每操作一次 -1.小于1时返回

    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    
    void a(void *arg)
    {
        int * sum = (int * )arg;
        if(*sum > 0)
        {
            printf("a --> %d
    ", sum[0]);
            sum[0] --;
        }
        pthread_exit(NULL);
    }
    
    void b(void * arg)
    {
        int * sum = (int * )arg;
        if(*sum > 0)
        {
            printf("b --> %d
    ", sum[0]);
            sum[0] --;
        }
        pthread_exit(NULL);
    }
    
    void c(void * arg)
    {
        int * sum = (int * )arg;
        if(*sum > 0)
        {
            printf("c --> %d
    ", sum[0]);
            sum[0] --;
        }
        pthread_exit(NULL);
    }
    
    int main(int argc, char const *argv[])
    {
        int sum = 10;
        pthread_t id1,id2,id3;
    
    
        while(sum > 0)
        {
            pthread_create(&id1,NULL,(void *)a,&sum);
            pthread_create(&id2,NULL,(void *)b,&sum);
            pthread_create(&id3,NULL,(void *)c,&sum);
        }
    
        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
        pthread_join(id3,NULL);
    
    
        exit(0);
    }

    按照一贯的思路,应该是由abc分别打印出10987654321,然鹅,运行结果:

     10被用了2次,到0的时候也没有立即返回。

    其实原理和我们上面画的那个图类似,在第一个线程还没有返回的时候,第二个线程已经开始了他的表演。

    这就造成了a,b都打出来了10的现象,后面也是相同的

    这个时候,我可以采取一个办法保护我们主函数中的sum,同时只允许一个线程操作sum

     那么我们就需要给这个变量上个锁。

    每当一个线程要操作sum之前,先给他上锁,在使用完成之后再解锁,

    在资源被锁住期间如果有其他线程想使用这个资源,那么就只能等待(阻塞),直到这个保护被取消,

    那么同样的,接下来使用资源的线程也要用上锁解锁的方式去保护这个资源

    这里我们就用到了互斥锁(互斥量)

     如果linux中man手册搜不到下列函数,可以安装sudo apt-get install manpages-posix-dev

     互斥锁创建、销毁

           #include <pthread.h>
    
           int pthread_mutex_destroy(pthread_mutex_t *mutex);
           int pthread_mutex_init(pthread_mutex_t *restrict mutex,
               const pthread_mutexattr_t *restrict attr);

    mutex  只想需要初始化的互斥量的指针

    attr      定义互斥锁的属性,如果填NULL 则设为默认值

        PTHREAD_MUTEX_INITIALIZER:创建快速互斥锁 (用了锁就要解,用一次就解一次)
        PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:创建递归互斥锁(嵌套锁,可以一个锁套一个锁)
        PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:创建检错互斥锁(如果同一个线程请求同一个锁,则报错。)
    成功返回0 ,失败返回 -1

    互斥锁上锁、解锁、判断有锁

           #include <pthread.h>
    
           int pthread_mutex_lock(pthread_mutex_t *mutex);
           int pthread_mutex_trylock(pthread_mutex_t *mutex);
           int pthread_mutex_unlock(pthread_mutex_t *mutex);

    参数同上

    返回值同上

    还用刚刚的例子,使用互斥锁打印sum的值

    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    //声明一个互斥锁 pthread_mutex_t lock;
    void a(void *arg) { int * sum = (int * )arg;
      //在使用之前上锁
    pthread_mutex_lock(&lock); if(*sum > 0) { printf("a --> %d ", sum[0]); sum[0] --; }
      //解锁
    pthread_mutex_unlock(&lock); pthread_exit(NULL); } void b(void * arg) { int * sum = (int * )arg; pthread_mutex_lock(&lock); if(*sum > 0) { printf("b --> %d ", sum[0]); sum[0] --; } pthread_mutex_unlock(&lock); pthread_exit(NULL); } void c(void * arg) { int * sum = (int * )arg; pthread_mutex_lock(&lock); if(*sum > 0) { printf("c --> %d ", sum[0]); sum[0] --; } pthread_mutex_unlock(&lock); pthread_exit(NULL); } int main(int argc, char const *argv[]) { int sum = 10; pthread_t id1,id2,id3;   //创建一个互斥锁 if (pthread_mutex_init(&lock, NULL) != 0) { perror("lock init failed"); exit(-1); } while(sum > 0) { pthread_create(&id1,NULL,(void *)a,&sum); pthread_create(&id2,NULL,(void *)b,&sum); pthread_create(&id3,NULL,(void *)c,&sum); } pthread_join(id1,NULL); pthread_join(id2,NULL); pthread_join(id3,NULL);   //进程即将结束,销毁互斥锁 pthread_mutex_destroy(&lock); exit(0); }

    这样就可以顺利打印出来正确的数值了

    使用互斥锁时要注意,避免在使用锁的过程中导致线程退出,那么就永远锁上了,别的线程无法获得已经死掉的线程的资源。

    死锁

    现在有2个线程,线程1和线程2,也有两把锁,锁A和锁B

    如果进入到一个状态:在使用锁A线程1在等待锁B,使用锁B线程2在等待锁A,那么这两个线程都无法进行下去,就进入了一个死锁的状态。

    产生死锁的必要条件:

    1、互斥条件:进程要求对所分配的资源进行排它性控制,即在一段时间内某资源仅为一进程所占用。
    2、请求和保持条件:当进程因请求资源而阻塞时,对已获得的资源保持不放。
    3、不剥夺条件:进程已获得的资源在未使用完之前,不能剥夺,只能在使用完时由自己释放。
    4、环路等待条件:在发生死锁时,必然存在一个进程--资源的环形链。

    预防死锁:
    1、设计获得锁的顺序

    2、超时放弃

    除了互斥锁之外,还可以用信号量来进行同步操作

    在进程学习中就提到过信号量的知识这里我们再复习一下

    信号量的原理就相当于一个计数器,先设定好一个最大的访问量。每当有人来访问,那么计数器-1.

    当信号量为0的时候,不允许其他线程来访问了,所以只能进入阻塞状态等待资源的释放。

    如果前面使用的人释放了这个资源,那么信号量+1,在排队的线程就可以获得使用的权限了。

    上面讲的获取和释放资源的过程,就是P操作V操作

    基本函数有下:

           #include <semaphore.h>
    
           int sem_init(sem_t *sem, int pshared, unsigned int value);//创建信号量 赋初值
    
           int sem_wait(sem_t *sem);//阻塞申请信号量,申请成功信号量-1(P操作)
    
           int sem_trywait(sem_t *sem);//非阻塞申请信号量,无论成功与否该函数会立即返回
    
           int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);//计时申请信号量,会阻塞
    
           int sem_post(sem_t *sem);//发送信号量(信号量+1)(V操作)
            
           int sem_getvalue(sem_t *sem, int *sval);//获取信号量的值
    
           int sem_destroy(sem_t *sem);//销毁信号量(不能再有线程申请信号量时销毁)

    sem      信号量指针

    pshared    0

    value     信号量初始值

    abs_timeout  唤醒的绝对时间

            struct timespec{

            time_t  tv_sec;   //秒

            long  tv_nsec;  //纳秒

            }

    sval      储存信号量的值的地址

    还是刚才的例子,我们用信号量操作一遍。。。

    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <semaphore.h>
    

    //声明一个信号量
    sem_t sem;
    void firstPthread(void *arg) { sem_wait(&sem);//阻塞等待信号量 请求成功后,信号量值-1 .等同于上锁 int *sum = (int *)arg; if(*sum > 0) { printf("firstPthread : %d ", sum[0]); sum[0]--; } sem_post(&sem);//释放,信号量+1,等同于解锁 pthread_exit(NULL); } void secondPthread(void *arg) { sem_wait(&sem); int *sum = (int *)arg; if(*sum > 0) { printf("secondPthread : %d ", sum[0]); sum[0]--; } sem_post(&sem); pthread_exit(NULL); } void thridPthread(void *arg) { sem_wait(&sem); int *sum = (int *)arg; if(*sum > 0) { printf("thridPthread : %d ", sum[0]); sum[0]--; } sem_post(&sem); pthread_exit(NULL); } int main(int argc, char const *argv[]) { int sum = 10; int ret = 0; pthread_t id1,id2,id3;   //初始化(二值)信号量 ret = sem_init(&sem,0,1); if(ret != 0) { perror("sem_init"); } while(sum > 0) { pthread_create(&id1,NULL,(void *)firstPthread,&sum); pthread_create(&id2,NULL,(void *)secondPthread,&sum); pthread_create(&id3,NULL,(void *)thridPthread,&sum); } pthread_join(id1,NULL); pthread_join(id2,NULL); pthread_join(id3,NULL);   //销毁信号量 sem_destroy(&sem); return 0; }

    运行结果:

    刚才的例子中我们只有一个资源sum = 10,当他减少到0的时候,子线程全部结束,进程结束。

    那么如果我现在想让他在sum == 0 的时候再次续命,此时重新赋值让sum = 10;

    那么该如何操作呢?

    这个时候就用到了条件变量

    使用起来也和前面的同步差不多,创建、销毁、等待、通知。

     常用函数如下:

    //动态创建条件变量
    int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
    

    //注销条件变量 int pthread_cond_destroy(pthread_cond_t *cond);

    //无条件等待,条件不满足时调用等待函数来等待条件满足,传参分别为条件变量和互斥锁 int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);

    //计时等待 int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);

    //广播条件变量成立信号,激活等待中的所有线程 int pthread_cond_broadcast(pthread_cond_t *cond);

    //发送单个条件变量成立信号,激活等待状态的一个线程 int pthread_cond_signal(pthread_cond_t *cond);

    cond  条件变量的指针

    attr     创建条件变量的属性,NULL默认值

    mutex  互斥锁指针

    abstime  唤醒的绝对时间 (timespec结构体在上文有表述)

    成功返回 0  失败返回 非0 

    还是前面的例子,我们用条件变量让sum == 10 的时候触发

    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    //互斥锁
    pthread_mutex_t lock;
    //条件变量
    pthread_cond_t cond;
    
    void a(void *arg)
    {
        int * sum = (int * )arg;
        //上锁
        pthread_mutex_lock(&lock);
        if(*sum > 0)
        {
            printf("a --> %d
    ", sum[0]);
            sum[0] --;
            //解锁
            pthread_mutex_unlock(&lock);
        }
        else
        {
            //先解锁
            pthread_mutex_unlock(&lock);
            //发送通知
            pthread_cond_signal(&cond);
            printf("send cond signal a
    ");
            sleep(1);
        }
        pthread_exit(NULL);
    }
    
    void b(void * arg)
    {
        int * sum = (int * )arg;
        pthread_mutex_lock(&lock);
        if(*sum > 0)
        {
            printf("b --> %d
    ", sum[0]);
            sum[0] --;
            pthread_mutex_unlock(&lock);
        }
        else
        {
            pthread_mutex_unlock(&lock);
            pthread_cond_signal(&cond);
            printf("send cond signal b
    ");
            sleep(1);
        }
        pthread_exit(NULL);
    }
    
    void c(void * arg)
    {
        int * sum = (int * )arg;
        pthread_mutex_lock(&lock);
        if(*sum > 0)
        {
            printf("c --> %d
    ", sum[0]);
            sum[0] --;
            pthread_mutex_unlock(&lock);
        }
        else
        {
            pthread_mutex_unlock(&lock);
            pthread_cond_signal(&cond);
            printf("send cond signal c
    ");
            sleep(1);
        }
        pthread_exit(NULL);
    }
    
    //等待条件线程
    //作用即为等待接收sign
    void waitPthread(void *arg)
    {
        //操作资源前要上锁
        pthread_mutex_lock(&lock);
        int * sum = (int *)arg;
        if(sum[0] > 0)
        {
            //在满足条件的情况下进入等待(挂起)状态
            pthread_cond_wait(&cond,&lock);
        }
        sum[0] = 10;
        //操作结束要解锁
        pthread_mutex_unlock(&lock);
        pthread_exit(NULL);
    }
    
    int main(int argc, char const *argv[])
    {
        int sum = 10;
        pthread_t id1,id2,id3,id4;
    
        //初始化互斥锁
        if (pthread_mutex_init(&lock, NULL) != 0)
        {
            perror("lock init failed");
            exit(-1);
        }
    
        //初始化条件变量
        if (pthread_cond_init(&cond, NULL) != 0)
        {
            perror("cond init failed");
            exit(-1);
        }
    
        //创建等待线程
        pthread_create(&id4,NULL,(void *)waitPthread,&sum);
    
        while(sum > 0)
        {
            pthread_create(&id1,NULL,(void *)a,&sum);
            pthread_create(&id2,NULL,(void *)b,&sum);
            pthread_create(&id3,NULL,(void *)c,&sum);
        }
    
        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
        pthread_join(id3,NULL);
        pthread_join(id4,NULL);
        //销毁互斥锁
        pthread_mutex_destroy(&lock);
        //销毁条件变量
        pthread_cond_destroy(&cond);
    
        exit(0);
    }

    运行结果如下:

    我们画图来解释一下这个过程:

    一步步分解一下他的运行状态:

    1、执行主线程,包括初始化函数

    2、执行等待线程,此时满足条件,所以进入等待状态。

    3、执行abc子线程,当sum>0时打印输出

    4、当sum<0 时,(图例中)由a线程发出了一个通知,唤醒了等待线程

    5、等待线程启动,将sum重置为10,同时结束了自己年轻的生命

    6、abc子线程继续打印输出

    7、sum又一次 小于0,此时abc子线程都发出了通知,但是等待线程已死。。。

    8、回收子线程,销毁通信资源。进程结束。

  • 相关阅读:
    思科交换机端口安全配置
    华为交换机端口安全配置
    多行文本出现省略号必备的条件(面试题)
    单行文本出现省略号必备的条件(面试题)
    让多个元素在一行显示的方法和技巧(面试题)
    overflow的多个作用
    雪碧图的使用和制作技巧
    列举background属性的8个属性值(面试题)
    background-origin设置背景图像的参考原点(位置)
    background-clip设置对象的背景图像向外裁剪的区域
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/13024309.html
Copyright © 2020-2023  润新知