My (Linux) application server has an (ANSI) C function on it that clients may access to perform number crunching. I understand the way servers generally work is they take requests from multiple clients and process these requests in parallel. That is, the same C function on the server may be called (and more importantly, may be run) as many times as there are clients wishing to use it (assuming sufficient system resources, e.g. CPU, memory, etc.).
The problem is one call to the C function uses practically the entire server resources. For the sake of discussion, assume the system crashes if two instances of the C function are run simultaneously.
QUESTION: Is there a way I can make sure that at most only one instance of this function is ever allowed to run (simultaneously)?
That’s what a mutex is for – to permit mutual exclusion. Only one instance/thread/process can lock a mutex. Other entities will fail when they try, and will continue to fail until the owner unlocks the mutex.