I've encountered a specific problem with my implementation and can't find a solution for it.
I have a two-part application. One part is a Java swing GUI. The second part is a C++ application that does all the (time consuming) calculation logic. The two processes communicate (in both directions) with their output and input streams. My problem now is that in one part of the C++ program I have to wait for user input coming from the Java program. However, waiting seems to block.
What works perfectly when I call the program in a shell is:
std::string inputLine1;
std::cin >> inputLine1;
When working with the Java UI, this does not work (naturally), because reading from std::cin is blocking, so when the C++ application waits for input, the Java application can't do anything.
Therefore I made another way of reading from std::cin that should (at least in my mind) work, but I can't make it work. It is:
std::string inputLine1;
while (true)
{
int c = std::cin.peek();
if (c != EOF)
{
std::cin >> inputLine1;
break;
}
std::this_thread::yield();
}
I also tried to replace the line with yield() with
std::this_thread::sleep_for(std::chrono::milliseconds(500));
In my mind, this code should work as following: I peek on std::cin. If something is there, I read it from cin. If nothing is there, I yield and try again later.
I know, yielding is considered to not be a very clean way of working, but I want to keep the communication between those two applications as simple as possible. No third party libraries, no more complex concepts (as Sockets), if possible.
However, this approach doesn't work, it gives the same behaviour as the first approach with just reading in from std::cin. The Java program becomes unresponsive and none of the two applications seem to do anything.
The C++ application works perfectly if called in a shell and if I provide the same input from the keyboard, so the problem shouldn't be there. If I delete all these given code snippets from the C++ application, the Java application is responsive and works - although it doesn't get the input it needs, obviously.
After trying for a long time to implement non-blocking input from cin, I'm pretty sure it's impossible to getting it to work consistently.
My current solution is to put the blocking cin into it's own tiny thread and let it do it's thing.
I've simplified my implementation a little bit for this example, as you need a thread safe storage system of some kind.