using fgets as non-blocking function c++

前端 未结 5 631
情书的邮戳
情书的邮戳 2020-12-12 01:23

I\'m writing a program that reads in a loop from the stdin, using the function fgets, as follows:

while(fgets(buffer2, BUFFERSIZE , stdin) != NULL){
  //So         


        
5条回答
  •  Happy的楠姐
    2020-12-12 02:05

    This would sound a little like overkill, but this is the one, that comes to my mind.

    Use 2 different threads - one using this loop and waiting blocking ( I don't think that this could be done non-blocking). And when something is read, push it into a pipe.

    Meanwhile, the other thread will do whatever it needs to do and check for data in the pipe from time to time ( apparently, you want this to be asynchronous, or at least I get it this way. If so, this means different threads )

    But then, you'll need to synchronize the two threads very well. You should check your OS about multithreading and IO operations.

提交回复
热议问题