Andna
Andna

Reputation: 6689

Adding watches to Inotify in multi-threaded program

I wanted to use inotify for monitoring some files in my C program.

I am wondering if it is safe to have one thread reading from inotify descriptor (the one returned by inotify_init) thus blocking until some event happens, during this waiting there would be a possibility of adding new file to watch queue using inotify_add_watch during the other thread waiting period.

Do I need to synchronize those actions or is it safe to do such thing?

Upvotes: 6

Views: 2395

Answers (1)

Syed H
Syed H

Reputation: 303

Don't have the exact answer, but I do know from experience that you can't even open files in another thread without triggering the read() in the thread you are using inotify. I recall reading that you need to use inotify_init1() along with the IN_CLOEXEC flag to allow file io in other threads. I'm not sure if that means you can actually use inotify in more than one thread simultaneously though.

Upvotes: 1

Related Questions