linux用互斥量进行同步

来源:互联网 发布:炉石传说淘宝金币号 编辑:程序博客网 时间:2024/05/08 02:13

#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <semaphore.h>void *thread_function(void *arg);pthread_mutex_t work_mutex; #define WORK_SIZE 1024char work_area[WORK_SIZE];int time_to_exit=0;int main(){   int res;   pthread_t a_thread;   void *thread_result;   res=pthread_mutex_init(&work_mutex,NULL);   if(res!=0)   {     perror("Mutex initialization failed");     exit(EXIT_FAILURE);   }   res=pthread_create(&a_thread,NULL,thread_function,NULL);   if(res!=0)   {      perror("Thread creation failed");     exit(EXIT_FAILURE);   }  pthread_mutex_lock(&work_mutex);  printf("Input some text, Enter 'end' to finish\n");  while(!time_to_exit)  {    fgets(work_area,WORK_SIZE,stdin);    pthread_mutex_unlock(&work_mutex);    while(1)    {      pthread_mutex_lock(&work_mutex);      if(work_area[0]!='\0')      {         pthread_mutex_unlock(&work_mutex);         sleep(1);      }      else        break;    }  }  pthread_mutex_unlock(&work_mutex);  printf("\nwaiting for thread to finish...\n");  res=pthread_join(a_thread,&thread_result);  if(res!=0)  {    perror("Thread join failed");    exit(EXIT_FAILURE);  }  printf("Thread joined\n");  pthread_mutex_destroy(&work_mutex);  exit(EXIT_SUCCESS);}void *thread_function(void *arg){  sleep(1);  pthread_mutex_lock(&work_mutex);  while(strncmp("end",work_area,3)!=0)  {    printf("You input %d characters\n",strlen(work_area)-1);    work_area[0]='\0';    pthread_mutex_unlock(&work_mutex);    sleep(1);    pthread_mutex_lock(&work_mutex);    while(work_area[0]=='\0')    {       pthread_mutex_unlock(&work_mutex);       sleep(1);       pthread_mutex_lock(&work_mutex);    }  }  time_to_exit=1;  work_area[0]='\0';  pthread_mutex_unlock(&work_mutex);  pthread_exit(0);}


在程序的开始,我们声明了一个互斥量,工作区和一个变量time_to_exit。

然后初始化互斥量,接下来启动新线程,新线程首先试图对互斥量加锁,如果它已经被锁住,这个调用将被阻塞直到它被释放为止,一旦获得访问权,我们就检查是否有申请退出程序的请求,如果有就设置time_to_exit变量,再把工作区的第一个字符设置为\0,然后退出。

如果不想退出,就统计字符个数,然后把work_area数组中的第一个字符设置为NULL,我们用将第一个字符设置为NULL的方法通知读取输入的线程,我们已完成了字符统计,然后解锁互斥量并等待主线程继续运行。我们将周期性的尝试给互斥量加锁,如果加锁成功,就检查是否主线程又有字符送来要处理,如果还没有就解锁互斥量继续等待,如果有,就统计字符个数并再次进入循环。


这种通过轮询来获得结果的方法通常并不是好的编程方式,在实际的编程中,我们应该尽量用信号量来避免出现这种情况。




原创粉丝点击