您现在的位置: 主页 > 嵌入式操作系统 > Linux > Linux下一个简单的多线程互斥锁的例子
本文所属标签:
为本文创立个标签吧:

Linux下一个简单的多线程互斥锁的例子

来源:net 网络用户发布,如有版权联系网管删除 2018-07-14 

本篇文章是对Linux下一个简单的多线程互斥锁的例子进行了分析介绍,需要的朋友可以参考下
 
复制代码 代码如下:


#include <stdio.h>
#include <pthread.h>
pthread_mutex_t Device_mutex ;
int count=0;
void thread_func1()
{
   while(1)
   {
       pthread_mutex_lock(&Device_mutex);
       printf("thread1: %dn",count);
       pthread_mutex_unlock(&Device_mutex);
       count++;
       sleep(1);
   }
}
void thread_func2()
{
   while(1)
   {
       pthread_mutex_lock(&Device_mutex);
       printf("thread2: %dn",count);
       pthread_mutex_unlock(&Device_mutex);
       count++;
       sleep(1);
   }
}
int main()
{
   pthread_t thread1, thread2;
   pthread_mutex_init(&Device_mutex,NULL);
   if(pthread_create(&thread1,NULL,(void*)thread_func1,NULL) == -1)
 {
  printf("create IP81 Thread error !n");
  exit(1);
 }
 sleep(1);
 if(pthread_create(&thread2,NULL,(void *)thread_func2,NULL) == -1)
 {
  printf("create IP81_2 Thread error!n");
  exit(1);
 }
 sleep(1);
 pthread_join(thread1,NULL);
 pthread_join(thread2,NULL);
 pthread_mutex_destroy(&Device_mutex);
 return 0;
}



              查看评论 回复



嵌入式交流网主页 > 嵌入式操作系统 > Linux > Linux下一个简单的多线程互斥锁的例子
 如下 代码 复制

"Linux下一个简单的多线程互斥锁的例子"的相关文章

网站地图

围观()