免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1845 | 回复: 3
打印 上一主题 下一主题

今天开始学线程,遇到个问题,请教一下! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-21 21:49 |只看该作者 |倒序浏览
我的目的:
在main里接收用户输入的一个char,把他存在一个全局的char型变量buff中,线程检查这个buff把它再打印出来。也就是个生产者消费者问题吧。我期望得到的输出是:
Entering new created thread...
please enter a character to main:   (a)   //这里用户假设输入a
main thread get  a
thread get a  from main
please enter a character to main:    //main中再等待用户输入,直到用户输入q为止。

但我得到的输出确是:
Entering new created thread...
please enter a character to main:a
thread get a from main
main thread get a
please enter a character to main:thread get
from main
main thread get
怎么会出现红色这几句??不明白 后面也一样,直到我输入q才又能产生一个我期望得到的输出。

please enter a character to main:b
main thread get b
thread get b from main
please enter a character to main:main thread get
下面是我的代码,请给指点指点:

  1. #include <pthread.h>
  2. #include <semaphore.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>


  5. void *thrd_routin(void * arg);
  6. char             buff;
  7. sem_t          local_sem;
  8. sem_t            full_sem;
  9. sem_t            empty_sem;
  10. int                    sem_var;
  11. int main()
  12. {
  13.     pthread_t   thdid;
  14.     int               res;
  15.     char            *thrdstatus;
  16.    

  17.    
  18.     sem_init(&local_sem,0,1);
  19.     sem_init(&full_sem,0,0);
  20.     sem_init(&empty_sem,0,1);

  21.     //sem_wait(&full_sem);
  22.     res = pthread_create(&thdid,NULL,(void *)thrd_routin,NULL);
  23.     if (res != 0)
  24.     {
  25.         perror("Creating thread failed!\n");
  26.         exit(-1);
  27.     }

  28. //char tmp;
  29.    
  30. do{       
  31.        

  32.         sem_wait(&empty_sem);       
  33.         sem_wait(&local_sem);
  34.       
  35.         printf("please enter a character to main:");
  36.         buff = getchar();

  37.         sem_post(&local_sem);
  38.         sem_post(&full_sem);

  39.         printf("main thread get %c\n",buff);

  40.     } while( buff != 'q' );

  41.     pthread_join(thdid,(void**)&thrdstatus);
  42.     sem_destroy(&local_sem);
  43.     exit(0);
  44. }

  45. void *thrd_routin( void *arg)
  46. {
  47.     printf("Entering new created thread...\n");
  48.        
  49.     while( buff != 'q')
  50.     {
  51.         sem_wait(&full_sem);
  52.         sem_wait(&local_sem);
  53.         printf("thread get %c from main\n",buff);
  54.         fflush(stdout);
  55.         sem_post(&local_sem);
  56.         sem_post(&empty_sem);
  57.     }
  58.     exit(0);
  59. }
复制代码

[ 本帖最后由 zhoufanking 于 2008-6-21 22:06 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-06-22 07:26 |只看该作者
怎么没人帮忙阿!!

论坛徽章:
0
3 [报告]
发表于 2008-06-23 09:53 |只看该作者
这个和多线程可没什么关系,而是你对getchar和键盘输入不了解。当你输入一个字符并按回车后,实际上是输入了两个字符,一个你输入的字符,另一个是回车。

论坛徽章:
0
4 [报告]
发表于 2008-06-23 10:06 |只看该作者
你用的是什么系统上的pthread.h ?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP