Reading both answers it appears that both answers provide a solution for creating a mutex but there are some cases where you need to use flock and some where you need to use mkdir:
- if you need already robust/tested features like timeout, blocking, etc etc --> use
flock
- builtin timeout
- builtin support for blocking or nonblocking
- builtin support for deleting the mutex after it is used
- if your distro does not have
flock --> you are forced to use mkdir
- if you need any of the timeout or other features provided by
flock you have to reinvent the wheel
- using
mkdir for creating a mutex means your code does not convey the purpose immediately --> whereas using flock means your code speaks for itself saying "this code is implementing synchronization"
- most people aren't familiar with
mkdir being a valid solution for creating a mutex and so using mkdir in this way may make your code have more code smell (especially if your distro has flock available and you choose not to use it)