手机
当前位置:查字典教程网 >操作系统 >unix linux >浅析Linux下一个简单的多线程互斥锁的例子
浅析Linux下一个简单的多线程互斥锁的例子
摘要:复制代码代码如下:#include#includepthread_mutex_tDevice_mutex;intcount=0;voidth...

复制代码 代码如下:

#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操作系统登录帐户的管理和审计

Redhat Linux下Bind的快速安装

U盘在Linux下显示不正常的解决方法

Linux下挂载硬盘分区的几种方法

linux下的用户权限

Linux中swap交换分区的创建和容量调整的教程

linux 2t快速分区过程介绍

在Linux下从视频提取音频的方法

Linux中安装oray服务的步骤分享

精品推荐
分类导航