LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 895|回复: 2

两个线程里都要读取键盘输入如何处理阿?

[复制链接]
发表于 2006-6-24 21:41:40 | 显示全部楼层 |阅读模式
没怎么写过多线程,用mutex锁了一下,但没锁住~~~
请教各位该如何做。
我的代码 大概像下面这个样子..l

thread1()
{
  pthread_mutex_lock(stdin_mutex);
    while((c=getchar())!=EOF)
    {
      getchar();
      pthread_mutex_unlock(stdin_mutex);
     switch(c)
           {
             ......
            .......
           }
          pthread_mutex_lock(stdin_mutex);
    }//while end
}

thread2()
{
     while (event_loop)
     {
      .....
       {
      //有时候需要读取键盘输入
     pthread_mutex_lock(stdin_mutex);
       fgets(.....);
      pthread_mutex_unlock(stdin_mutex);
       }
     ......
      }
}
发表于 2006-6-25 15:13:51 | 显示全部楼层
就算你锁住了,万一你每次读到的都应该是另一个线程想要的呢?我觉得不应该这么做。你想达到什么目的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-25 16:37:14 | 显示全部楼层
就是我的程序里有两个子线程 :一个负责用户接口,另一个在后台处理事件循环,但现在后台线程里也偶尔要有输入~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表