使用 pthreads 查看 stdin
我正在尝试查看标准输入,看看是否有任何使用 pthreads 的东西。我(认为我)需要这样做,因为如果 std in 中没有任何内容,流访问函数将阻止输入。
我觉得执行此操作的方法是启动一个检查 stdin 的 pthread,然后 sleep(1) 并查看线程是否发现任何内容。
这是我到目前为止所拥有的。如果没有任何内容通过管道传输到程序中,那么它将按预期休眠,但如果标准输入中有内容,则线程永远不会被触发。
#include <iostream>
#include <pthread.h>
#include <stdlib.h>
using namespace std;
void *checkStdIn(void *d){
char c = '\0';
c = cin.peek();
if (c){
cout << c << endl;
}
}
int main(int argc, char *argv[]){
pthread_t thread;
int rc;
rc = pthread_create(&thread, NULL, checkStdIn, NULL);
if (rc){
cerr << "Error no. " << rc << endl;
exit(EXIT_FAILURE);
}
sleep(2);
return 0;
}
I'm trying to peek stdin to see if anything is there using pthreads. I (think I) need to do this because if there is nothing in std in, stream access functions will block for input.
I feel the way to do this is to fire off a pthread that checks stdin, and sleep(1) and see if the thread found anything.
Here's what I have so far. If nothing is piped into the program then it will sleep as expected, but if something is in stdin the thread never gets fired.
#include <iostream>
#include <pthread.h>
#include <stdlib.h>
using namespace std;
void *checkStdIn(void *d){
char c = '\0';
c = cin.peek();
if (c){
cout << c << endl;
}
}
int main(int argc, char *argv[]){
pthread_t thread;
int rc;
rc = pthread_create(&thread, NULL, checkStdIn, NULL);
if (rc){
cerr << "Error no. " << rc << endl;
exit(EXIT_FAILURE);
}
sleep(2);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您不需要 pthreads,您可以使用
select(2)
或poll(2)
来了解是否可以在不阻塞应用程序的情况下查看标准输入。为此,我编写了my_peek()
函数,您只需传递您想要等待输入的秒数(如果您不想等待,甚至可以传递 0):请请注意,依赖
select(2)
来判断cin
对象或stdin
中是否有数据FILE< /code> 结构,因为正如尼莫所说,它们是缓冲。最好的办法是在本例中使用
read(2)
避免使用“cin”。my_peek()
的改进版本如下所示:有关详细信息,请查看位于 http://linux.die.net/man/2/select。
PS:您可以尝试依赖
std::cin.rdbuf()->in_avail()
返回的值,如 cpluplus 网站 http://www.cplusplus.com/reference/iostream/streambuf/in_avail/,甚至在istream
类的readsome()
方法,但它们通常依赖于 GNU C++ 库中未公开的FILE
缓冲区。不要这样做,否则你可能会失败。You don't need pthreads for that and you can use
select(2)
orpoll(2)
to known if you can peek the stdin without blocking your application. For that I coded themy_peek()
function which you just need to pass the number of seconds you want to wait for the input (you can even pass 0 if you don't want to wait):Please note that it is BAD to rely on
select(2)
to tell if there is data in thecin
object orstdin
FILE
structure because, as Nemo stated, they are BUFFERED. The best thing to do is avoid "cin" in this example usingread(2)
. The improved version ofmy_peek()
would look like:For more information please check the
select(2)
manual page at http://linux.die.net/man/2/select.PS: You could try to rely on the value returned by the
std::cin.rdbuf()->in_avail()
as explained in the cpluplus site http://www.cplusplus.com/reference/iostream/streambuf/in_avail/, or even in thereadsome()
method of theistream
class, but they usually depend on theFILE
buffer which is not exposed in the GNU C++ library. Don't do it or you might fail.您的
sleep(2)
毫无价值,因为它不能保证您的线程将在程序终止前 2 微秒内完成。您需要实现pthread_join(thread, NULL);
来等待线程完成。请参阅此处,了解一个很好的 pthread 示例。另外,cin.peek() 将阻塞等待输入。它就是这样设计的。请参阅此处的 cin.peek 示例。
Your
sleep(2)
is worthless because it does not guarantee that your thread will finish within 2 microseconds prior to program termination. You need to implementpthread_join(thread, NULL);
to wait for the thread to finish. See here for a good pthread example.Also, cin.peek() will block waiting for input. That is how it is designed. See here for an example of cin.peek.
编辑: Bawh,费尔南多 (Fernando) 的忍者 :)
好的,所以我不能 100% 确定最适合您的答案是什么,因为我无法告诉您最终希望程序做什么做。
首先,这不是应该使用线程解决的问题。线程并不是包罗万象的解决方案,与其他解决方案相比,线程通常会产生巨大的开销。因为您已经在使用
pthreads
,所以我假设 Windows 兼容性不是问题。第一步是禁用规范模式,这将允许您获取字符而无需等待
enter
。如果您 100% 确定stdin
永远不会成为终端,则可以跳过此步骤。Edit: Bawh, ninja'd by Fernando :)
Okay, so I'm not 100% sure on what the best answer will be for you because I can't tell what you want your program to eventually do.
First and foremost, this is not the kind of problem that should be solved using threads. Threads are not the catch-all solution to everything and generally have huge overhead compared to other solutions. Because you're already using
pthreads
, I'll assume Windows compatibility isn't an issue.The first step is to disable canonical mode, which will allow you to gett characters without having to wait for
enter
. If you were 100% surestdin
won't ever be a terminal, you can skip this step.