File Module Mutual exclusion?

Is the file module provided by elixir, writes to file in mutual exclusion.?
https://hexdocs.pm/elixir/1.12/File.html#content

You can open a file with the exclusive mode File — Elixir v1.12.3

File.open("/path/to/file", [:exclusive])
1 Like

:file is it same for in erlang :file?


I want to write to file in mutual exclusion

It is exactly what @benwilson512 have shown.

2 Likes

Then use the :exclusive option. It does what you want. Of course unless the underlying filesystem does not support it. And that is what the warning is about.

1 Like

Just one comment, :exclusive has the same meaning of the O_EXCL flag in the C system call. Somewhat confusingly, it does NOT mean that the file is opened with exclusive read or write access: other processes can still both read and write such file while it is still open by the original creator. What it really means, is that the file will be created, or the call with fail if the file already exists.

The “exclusive” name comes from the fact that one of its primary uses is to create lock files, which in turn are used to implement mutual exclusion.

The Erlang documentation is correct, but I got confused in the past by the name exclusive, thinking it meant exclusive access, therefore I am writing this in the hope to save someone else some head scratching.

5 Likes

Thank you everyone for helping me out.
Have a nice day : )