Hey everyone
I have an issue where I need non blocking IO, ie, the program will still continue while waiting on user input. Let me start straight up by saying that threads is not an option.
Here's what I have for a concept:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int main()
{
int i = 0; // Purpose is to show that the io is non-blocking
char str[24] = {0};
while (true)
{
for (int j = 0; j < 1000000000; j++); // Purely to make the output readable before it disappears off screen
cin.readsome(str, 24);
cout << i << str << endl;
}
return 0;
}
|
Since
readsome
will only take what is in the input buffer, and finish when it is empty, i figured it would not put anything into
str
if there was not yet anything entered. If something had been entered in, then it would put it in, and execution altered appropriately.
Unfortunately, when this code is run, whenever data is entered, only the counter is printed, ie, nothing is going into str.
Is there something I missed and / or is there a better way to do non blocking IO with iostream?
Cheers
Damian