I don't have the code yet because I'm about to write it(actually, I've a very different code but the problem is that I need the timeout mechanism, otherwise the program can't cooperate properly). But I'll quote my problem again:
"I need this to be able to communicate with another program in a single thread without blocking forever and without race issues." You gave me a good idea - I might just let the stdin-thread run forever and every time it receives something it'll cache the message into a shared resource - it won't block the main thread and I just need to implement the part where the mainloop only waits for the data to appear in the shared buffer.