淺析Linux下一個(gè)簡單的多線程互斥鎖的例子
更新時(shí)間:2013年07月09日 11:21:08 作者:
本篇文章是對(duì)Linux下一個(gè)簡單的多線程互斥鎖的例子進(jìn)行了分析介紹,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
#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: %d\n",count);
pthread_mutex_unlock(&Device_mutex);
count++;
sleep(1);
}
}
void thread_func2()
{
while(1)
{
pthread_mutex_lock(&Device_mutex);
printf("thread2: %d\n",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;
}
相關(guān)文章
linux入門教程 第5章:網(wǎng)絡(luò)應(yīng)用
linux入門教程 第5章:網(wǎng)絡(luò)應(yīng)用...2006-10-10
Linux系統(tǒng)中xorg.conf文件詳細(xì)介紹
以下是對(duì)Linux系統(tǒng)中的xorg.conf文件進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08
統(tǒng)計(jì)Linux 中文件和文件夾/目錄的數(shù)量(示例代碼)
這篇文章主要介紹了統(tǒng)計(jì)Linux 中文件和文件夾/目錄的數(shù)量(示例代碼),需要的朋友可以參考下2018-02-02
關(guān)于Linux服務(wù)器磁盤空間占滿問題的解決方法
下面給大家分享一篇關(guān)于Linux服務(wù)器磁盤占滿問題解決方法(/dev/sda3 滿了),需要的的朋友參考下吧2017-05-05

