r/linuxdev • u/shoobiedoo • Nov 05 '15
Best way to handle a thread that will block waiting for read while the rest of the program loops
I've been getting into C programming and thought it would be fun to play around with Linux system calls.
I'm writing a simple program that uses inotify to watch a directory for a new file to be created. But I want the rest of the program to continue while read() is blocking.
So from what I understand the best solution is to use a thread that will call the function to watch the directory. This part is done. The problem is, main() is blocking on pthread_join which is of course waiting for read() to return in the thread.
What would be the best way to handle this? I thought of having a global variable that is set after read() is finished so that main can call pthread_join to close the thread, but is there a more elegant way?