My main thread looks like this:
while (!shouldExit) {
int c = getchar();
switch (c) {
case .... :
case .... :
case 'x' : shuoldExit = true; break;
}
}
Then , there is another thread that reads data from the network, and when I get a special command it sets shouldExit = 1
. This works perfectly, the only problem is that getchar()
according to strace
keeps blocking on read(0)
.
How can my working thread abort the read
system call in the main thread?
I tried sending signals to myself, but that just kills the application (expected). When I trap the signals in a null handler the app keeps alive, but the read
keeps blocking.
One option might be to use select()
, but I am not happy about it. Any alternatives?
EDIT:
See also this question, which is similar to mine, but we are searching for different solutions. His solution is not applicable to my problem, as the blocking read()
is on the main thread.