I have some resource which I'd like to protect from concurrent usage both by threads in the same process and among different processes. What is the "right" way of doing it in *nix?
E.g. we want to write to a file from a multithreaded app, which once in a while forks some subprocesses. How to ensure that each thread has exclusive access to the file for some time? I'm looking for a general answer, not something that works only for writing to a file, since it may also be a shared memory segment etc.