I have RUST library that writes some tracking data to a file or a linux pipe.
The library is meant to work in a multi-threaded and multiprocess environment writing to the same file or linux pipe
Each write, writes a line of the form
<uuid> <operation> <somedata>
file : File::create(&fname).unwrap()
I can limit the size of the line to some max value if needed, to make sure it is not interleaved
I know that when writing to linux pipe, there is PIPE_BUF is 4,096 bytes, under which it is guaranteed not to be interleaved.
I am finding that when I write with RUST, and I run my test suite without "cargo test -- --test-threads=1"
I am seeing interleaving of the output as follows
5c3680a0a6fc4d8ab69998d50bd069ac5c3680a0a6fc4d8ab69998d50bd069ac test_003test_004 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 5c3680a0a6fc4d8ab69998d50bd069ac5c3680a0a6fc4d8ab69998d50bd069ac test_003test_004 **DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
instead of seeing
5c3680a0a6fc4d8ab69998d50bd069ac test_004 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 5c3680a0a6fc4d8ab69998d50bd069ac test_003 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 5c3680a0a6fc4d8ab69998d50bd069ac test_004 *DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 5c3680a0a6fc4d8ab69998d50bd069ac test_003 *
Basically the writes are interleaved.
Without "--test-threads=1" the tests are passing.
I can do a Mutex lock for these writes. I am trying not to do that.
1. Can I expect writes to not be interleaved for regular files?
2. If I cant how do log files manage to do this? Do they lock? Logs from multi-threaded programs seem to not be jumbled.
3. Can I expect writes to not be interleaved for linux pipes?