设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

linux下的C语言开发(线程互斥)

2012-2-24 10:47| 发布者: 红黑魂| 查看: 3597| 评论: 0|来自: csdn博客

摘要: 对于编写多线程的朋友来说,线程互斥是少不了的。在linux下面,编写多线程常用的工具其实是pthread_mutex_t。本质上来说,它和Windows下面的mutex其实是一样的,差别几乎是没有。希望对线程互斥进行详细了解的朋友可 ...

 对于编写多线程的朋友来说,线程互斥是少不了的。在linux下面,编写多线程常用的工具其实

是pthread_mutex_t。本质上来说,它和Windows下面的mutex其实是一样的,差别几乎是没有。

希望对线程互斥进行详细了解的朋友可以看这里

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6. static int value = 0;  
  7. pthread_mutex_t mutex;  
  8.   
  9. void func(void* args)  
  10. {  
  11.     while(1)  
  12.     {  
  13.         pthread_mutex_lock(&mutex);  
  14.         sleep(1);  
  15.         value ++;  
  16.         printf("value = %d!\n", value);  
  17.         pthread_mutex_unlock(&mutex);  
  18.     }  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     pthread_t pid1, pid2;  
  24.     pthread_mutex_init(&mutex, NULL);  
  25.   
  26.     if(pthread_create(&pid1, NULL, func, NULL))  
  27.     {  
  28.         return -1;  
  29.     }  
  30.   
  31.     if(pthread_create(&pid2, NULL, func, NULL))  
  32.     {  
  33.         return -1;  
  34.     }  
  35.   
  36.     while(1)  
  37.         sleep(0);  
  38.   
  39.     return 0;  
  40. }  
   

酷毙
2

雷人

鲜花

鸡蛋

漂亮

刚表态过的朋友 (2 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部