使用 pthreads 查看 stdin

发布于 2024-11-26 21:34:44 字数 759 浏览 1 评论 0原文

我正在尝试查看标准输入,看看是否有任何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂ガ小子 2024-12-03 21:34:44

为此,您不需要 pthreads,您可以使用 select(2)poll(2) 来了解是否可以在不阻塞应用程序的情况下查看标准输入。为此,我编写了 my_peek() 函数,您只需传递您想要等待输入的秒数(如果您不想等待,甚至可以传递 0):

/* According to POSIX.1-2001 */
#include <sys/select.h>

/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <iostream>

int
my_peek(unsigned int nsecs)
{
    struct timeval timeout;
    fd_set rfds;
    int fd;

    // stdin file descriptor is 0
    fd = 0;

    timeout.tv_sec = nsecs;
    timeout.tv_usec = 0;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    if (select(fd + 1, &rfds, NULL, NULL, &timeout) > 0)
        return std::cin.peek();
    return -1;
}

int
main(void)
{
    int peek;

    peek = my_peek(2);
    if (peek != -1) {
        std::cout << "we could peek without freezing" << std::endl;
        std::cout << "my_peek() returned " << peek << std::endl;
    } else {
        std::cout << "we could not peek without freezing" << std::endl;
    }

    return 0;
}

请请注意,依赖 select(2) 来判断 cin 对象或 stdin 中是否有数据 FILE< /code> 结构,因为正如尼莫所说,它们是缓冲。最好的办法是在本例中使用 read(2) 避免使用“cin”。 my_peek() 的改进版本如下所示:

int
my_peek(unsigned int nsecs)
{
    struct timeval timeout;
    fd_set rfds;
    int fd;
    unsigned char c;

    // stdin file descriptor is 0
    fd = 0;

    timeout.tv_sec = nsecs;
    timeout.tv_usec = 0;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    if (select(fd + 1, &rfds, NULL, NULL, &timeout) <= 0)
        return -1;
    if (read(fd, &c, 1) != 1)
        return -1;
    return static_cast<int>(c); /* or "return (int)c" for C-only programs */
}

有关详细信息,请查看位于 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) or poll(2) to known if you can peek the stdin without blocking your application. For that I coded the my_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):

/* According to POSIX.1-2001 */
#include <sys/select.h>

/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <iostream>

int
my_peek(unsigned int nsecs)
{
    struct timeval timeout;
    fd_set rfds;
    int fd;

    // stdin file descriptor is 0
    fd = 0;

    timeout.tv_sec = nsecs;
    timeout.tv_usec = 0;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    if (select(fd + 1, &rfds, NULL, NULL, &timeout) > 0)
        return std::cin.peek();
    return -1;
}

int
main(void)
{
    int peek;

    peek = my_peek(2);
    if (peek != -1) {
        std::cout << "we could peek without freezing" << std::endl;
        std::cout << "my_peek() returned " << peek << std::endl;
    } else {
        std::cout << "we could not peek without freezing" << std::endl;
    }

    return 0;
}

Please note that it is BAD to rely on select(2) to tell if there is data in the cin object or stdin FILE structure because, as Nemo stated, they are BUFFERED. The best thing to do is avoid "cin" in this example using read(2). The improved version of my_peek() would look like:

int
my_peek(unsigned int nsecs)
{
    struct timeval timeout;
    fd_set rfds;
    int fd;
    unsigned char c;

    // stdin file descriptor is 0
    fd = 0;

    timeout.tv_sec = nsecs;
    timeout.tv_usec = 0;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    if (select(fd + 1, &rfds, NULL, NULL, &timeout) <= 0)
        return -1;
    if (read(fd, &c, 1) != 1)
        return -1;
    return static_cast<int>(c); /* or "return (int)c" for C-only programs */
}

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 the readsome() method of the istream class, but they usually depend on the FILE buffer which is not exposed in the GNU C++ library. Don't do it or you might fail.

攒眉千度 2024-12-03 21:34:44

您的 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 implement pthread_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.

薯片软お妹 2024-12-03 21:34:44

编辑: Bawh,费尔南多 (Fernando) 的忍者 :)

好的,所以我不能 100% 确定最适合您的答案是什么,因为我无法告诉您最终希望程序做什么

首先,这不是应该使用线程解决的问题。线程并不是包罗万象的解决方案,与其他解决方案相比,线程通常会产生巨大的开销。因为您已经在使用 pthreads,所以我假设 Windows 兼容性不是问题。

第一步是禁用规范模式,这将允许您获取字符而无需等待 enter。如果您 100% 确定 stdin 永远不会成为终端,则可以跳过此步骤。

#include <iostream>
#include <termios.h>

void toggle_canonical(bool on) {
    struct termios terminal_settings;

    // Get the existing terminal settings
    tcgetattr(0 /* stdin */, &terminal_settings);
    if(on) {
        // Disable canonical mode
        terminal_settings.c_lflag &= ~ICANON;
        // Read at least one character.
        terminal_settings.c_cc[VMIN] = 1;
    } else {
        // Enable canonical mode
        terminal_settings.c_lflag |= ICANON;
    }
    tcsetattr(0 /* stdin */, TCSANOW, &terminal_settings);

    return;
}

int main(const int argc, const char* argv[]) {
    // The read timeout, which can be 0 if you don't want to block at all.
    struct timeval to = {5 /* seconds */, 0 /* miliseconds */};
    fd_set read_fds;
    int ret = 0;

    // Turn canonical mode on
    toggle_canonical(true);

    FD_ZERO(&read_fds);
    FD_SET(0, &read_fds);

    // The first parameter to select() is the highest file
    // descriptor in the set + 1, so in this case because
    // STDIN == 0, we pass 1. This is actually completely
    // ignored on several platforms, including Windows.
    if((ret = select(1, &read_fds /* read set */, NULL /* write set */, NULL /* error set */, &to /* timeout */)) == 0) {
        std::cout << "You didn't type anything in time." << std::endl;
    } else if (ret == 1) {
        std::cout << "Yay, you typed something in time!" << std::endl;
    } else if (ret == -1) {
        std::cout << "Oh no, an error occured!" << std::endl;
    }

    // Turn canonical mode off
    toggle_canonical(false);
    return 0;
}

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% sure stdin won't ever be a terminal, you can skip this step.

#include <iostream>
#include <termios.h>

void toggle_canonical(bool on) {
    struct termios terminal_settings;

    // Get the existing terminal settings
    tcgetattr(0 /* stdin */, &terminal_settings);
    if(on) {
        // Disable canonical mode
        terminal_settings.c_lflag &= ~ICANON;
        // Read at least one character.
        terminal_settings.c_cc[VMIN] = 1;
    } else {
        // Enable canonical mode
        terminal_settings.c_lflag |= ICANON;
    }
    tcsetattr(0 /* stdin */, TCSANOW, &terminal_settings);

    return;
}

int main(const int argc, const char* argv[]) {
    // The read timeout, which can be 0 if you don't want to block at all.
    struct timeval to = {5 /* seconds */, 0 /* miliseconds */};
    fd_set read_fds;
    int ret = 0;

    // Turn canonical mode on
    toggle_canonical(true);

    FD_ZERO(&read_fds);
    FD_SET(0, &read_fds);

    // The first parameter to select() is the highest file
    // descriptor in the set + 1, so in this case because
    // STDIN == 0, we pass 1. This is actually completely
    // ignored on several platforms, including Windows.
    if((ret = select(1, &read_fds /* read set */, NULL /* write set */, NULL /* error set */, &to /* timeout */)) == 0) {
        std::cout << "You didn't type anything in time." << std::endl;
    } else if (ret == 1) {
        std::cout << "Yay, you typed something in time!" << std::endl;
    } else if (ret == -1) {
        std::cout << "Oh no, an error occured!" << std::endl;
    }

    // Turn canonical mode off
    toggle_canonical(false);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文