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

本篇文章是对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;
}

(0)

相关推荐

  • linux下一个网卡配置多个IP

    linux下一个网卡如何配置多个IP?linux下边如果没有图形界面了配置网络还真的不是太方便了,呵呵,习惯问题,当然习惯需要改的,最近遇到一个问题需要单网卡配置多个ip地址。文本控制台下面有netc ...

  • Linux下如何实现shell多线程编程以提高应用程序的响应

    Linux中多线程编程拥有提高应用程序的响应、使多cpu系统更加有效等优点,下面小编将通过Linux下shell多线程编程的例子给大家讲解下多线程编程的过程,一起来了解下吧。 #!/bin/bash ...

  • Linux下的多线程编程和fork()函数详解

     一.fork()函数 在操作系统的基本概念中进程是程序的一次执行,且是拥有资源的最小单位和调度单位(在引入线程的操作系统中,线程是最小的调度单位).在Linux系统中 创建进程有两种方式:一是由操作 ...

  • Linux下基于socket多线程并发通信的实现

    Linux下基于socket多线程并发通信的实现 分类: Linux2011-05-21 18:13 8455人阅读 评论(5) 收藏 举报socket多线程linuxserverstruct服务器[ ...

  • linux下mysql的搭建及简单配置

    linux系统是企业及网站应用非常普遍的系统:mysql又是一款非常优秀的免费数据库,二者合一是目前大多数企业建站的首选,在此介绍一下linux下搭建mysql环境以及一些简单配置 操作方法 01 首 ...

  • linux环境下一个进程最多能有多少个线程

    以下是对在linux环境下一个进程最多能有多少个线程进行了介绍,需要的朋友可以过来参考下 默认情况下: 主线程+辅助线程 +<253个自己的线程<=255 含主线程和一个辅助线程,最多25 ...

  • Linux下Intellij idea2017安装和破解激活的最简单详细教程(附下载)

    IntelliJ IDEA是一款JetBrains公司开发的Java ide开发工具,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各 ...

  • Linux下安装TeamCity简单教程

    本文下载的TeamCity路径为/usr/local/src/ # cd /usr/local/src/TeamCity # ./bin/teamcity-server.sh start 显示如下: ...

  • linux下如何创建一个.txt类型的文件?

    本文介绍linux下如何创建一个.txt类型的文件的方法: 1.vi 文件名.txt: 2.touch 文件名.txt 操作方法 01 [步骤一]vi 文件名.txt 比如创建文件file.txt,用 ...