Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226180
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 848
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 10:27
文章分类
文章存档

2014年(9)

2013年(66)

我的朋友

分类: C/C++

2013-11-12 14:26:56

Windows中的WaitForSingleObject()函数对应在Linux中的sem_wait(),SetEvent对应sem_post(),
Windows中的WaitForSingleObject()函数对应在vxworks中semTake(),SetEvent对应semGive().

参考下面的Linux程序:

 

  1. char tem[10]; //读写公共区  
  2. sem_t sem;  
  3. void* thread_fun(void*);  
  4. int main()  
  5. {  
  6. int counter=0;  
  7. pthread_t mythread;  
  8. sem_init(&sem,0,0);  
  9. pthread_create(&mythread,NULL,thread_fun,NULL);  
  10. while(counter<10) //往读写区里写10次'f'  
  11. {  
  12. tem[counter]='f';  
  13. counter++;  
  14. sem_post(&sem);  
  15. }  
  16. pthread_join(mythread,NULL); //等待子线程  
  17. sem_destroy(&sem);  
  18. exit(0);  
  19. }  
  20. void* thread_fun(void* arg) //子线程函数  
  21. {  
  22. int counter=0;  
  23. while(counter<10&&sem_wait(&sem)==0)  
  24. {  
  25. printf("%c",tem[counter]); //读出来显示  
  26. counter++;  
  27. //sem_wait(&sem);  
  28. }  
  29. pthread_exit(NULL);  
  30. }  
阅读(2431) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~