国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

淺析Linux下一個簡單的多線程互斥鎖的例子

2020-10-28 18:49:43
字體:
供稿:網(wǎng)友
復(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;
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤凰县| 甘洛县| 达拉特旗| 贞丰县| 中方县| 墨玉县| 三门峡市| 济宁市| 通道| 汉阴县| 乐亭县| 牡丹江市| 柳江县| 玛沁县| 固阳县| 太白县| 琼结县| 特克斯县| 隆尧县| 乌苏市| 楚雄市| 汝城县| 社旗县| 泰顺县| 祁连县| 夏津县| 温泉县| 永年县| 仙居县| 九寨沟县| 兴海县| 保德县| 瑞昌市| 荣昌县| 横山县| 元朗区| 金阳县| 宁波市| 安塞县| 南城县| 应城市|