第三季-第21课-多线程同步

第21课-多线程同步

 

21.1 线程同步

多个线程按照规定的顺序来执行,就是线程同步

安排的情景:现在有A和B两个同学要打扫卫生。A同学要扫地,扫5次;B同学要擦地。我们知道肯定是要A同学先扫地才可以。

21.2 综合实例

1. 初始例程

sync.c

#include<pthread.h>

#include<stdio.h>

pthread_mutex_t mut;

int number = 0;

pthread_t thread[2];

void stduentA()

{

         int i;

         for(i=0;i<5;i++)

         {

                  pthread_mutex_lock(&mut);

                  //扫1次地

                  number++;

                  if(number>=5)

                  {

                           printf("studentA has finished his work! ");

                  }

                  pthread_mutex_unlock(&mut);

                  //休息1秒钟

                  sleep(1);  

         }

         //退出

         pthread_exit(NULL);

}

void stduentB()

{

         while(1)

         {

                  pthread_mutex_lock(&mut); 

                  if(number>=5)//判断A同学是否扫完了5次地

                  {

                  //拖地

                           number = 0;

                           printf("studentB has finished his work! ");

                           break;

                  }

                  else

                  {

                  pthread_mutex_unlock(&mut);

                           //睡眠2秒钟

                           sleep(2);

                  }

         }

         //退出

         pthread_exit(NULL);

}

int main()

{

         //初始化互斥锁

         pthread_mutex_init(&mut, NULL);

         //创建A同学线程

         pthread_create(&thread[0], NULL, stduentA, NULL);

         //创建B同学线程

         pthread_create(&thread[1], NULL, stduentB, NULL);

         //等待A线程结束

         pthread_join(thread[0], NULL);

         //等待B线程结束

         pthread_join(thread[1], NULL);

}

运行结果:studentA has finished his work!

studentB has finished his work!

2. 改进例程

上面的程序虽然已经达成了我们期望的结果,但是StudenB程序运行的效率低下,并不是我们期望的。

为了达到高效的操作,我们下面学习一下条件变量。

条件变量

初始化:pthread_cond_t cond_ready=PTHREAD_COND_INITIALIZER;

等待条件成熟:pthread_cond_wait(&cond_ready,&mut);

设置条件成熟:pthread_cond_signal(&cond_ready);

l  条件变量的机制

我们之前的程序是先让A线程执行,B线程一直在查询成功的条件,而且要有休眠的体现,这一切的操作都是消耗CPU的。为了减少CPU的占用率,需要一种通知机制。在A线程运行的时候,B线程可以一直睡眠                 。当A进程完成运行,呼叫B进程让B进程运行即可。

sync.c

#include<pthread.h>

#include<stdio.h>

pthread_mutex_t mut;

int number = 0;

pthread_t thread[2];

pthread_cond_t cond_ready=PTHREAD_COND_INITIALIZER; //设置机制

void stduentA()

{

         int i;

         for(i=0;i<5;i++)

         {

                  pthread_mutex_lock(&mut);

                  //扫1次地

                  number++;

                  if(number>=5)

                  {

                           printf("studentA has finished his work! ");

                           //通知B

                           pthread_cond_signal(&cond_ready);//成熟机制

                  }

                  pthread_mutex_unlock(&mut);

                  //休息1秒钟

                  sleep(1);  

         }

         //退出

         pthread_exit(NULL);

}

void stduentB()

{

         pthread_mutex_lock(&mut); 

         if(number<5)//判断A同学是否扫完了5次地

                  pthread_cond_wait(&cond_ready,&mut); //等待休眠

         number = 0;

         pthread_mutex_unlock(&mut);

         printf("studentB has finished his work! ");

         //退出

         pthread_exit(NULL);

}

int main()

{

         //初始化互斥锁

         pthread_mutex_init(&mut, NULL);

         //创建A同学线程

         pthread_create(&thread[0], NULL, stduentA, NULL);

         //创建B同学线程

         pthread_create(&thread[1], NULL, stduentB, NULL);

         //等待A线程结束

         pthread_join(thread[0], NULL);

         //等待B线程结束

         pthread_join(thread[1], NULL);

}

运行结果:studentA has finished his work!

studentB has finished his work!

可以更快的显示!

原文地址:https://www.cnblogs.com/free-1122/p/11352996.html