currently I am doing.
int main()
{
create_daemon_thread_and_run();
getchar();
return 0;
}
I use a getchar() to block main thread terminate entire process.
I don’t like getchar() because if I type something to stdin, it will return and process will terminate. But I want it block forever, something like
while()
{
;
}
But I have concern while loop forever will eat up CPU useage. I want something just put the thread resting, and don’t eat CPU. And I want cross-platform way to do so.
You’d probably be better of using pthread_join (if it was a pthread, non-detached thread).
Otherwise, use a mutex, condition variable, or sempahore.
The following sample demonstrates how to use a semaphore, and it responds to signals (e.g. when Ctrl-C is used in the terminal). Of course, your ‘demon’ implementations is free to signal the semaphore when it’s done processing and wants to shut down.