I’m writing something utilizing the libonion library, which is multithreaded, and was just wondering how you would go about detecting whether any file pointers declared are currently being read by any other threads or not.
(Basically I want to create manually-handled temp files where they’re removed if no one’s reading from them anymore.)
File pointers are just a pointer to a file resource and whether some other thread is reading it cannot be determined by just looking at the pointer. You may want to declare a mutex and a global lock-count alongside the file-pointer. When ever any thread is intending to read it, it can obtain a shared lock on the mutex increase the lock-count and release it after decrementing the lock-count when the thread has no intention to further read the file.
You can query this lock-count to see if the value is zero, in which case you can safely release it.
Also note, as this lock-count is global and you can end up in a race condition, reading and updating the lock-count variable should be done exclusively.