Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is os.File's Write() threadsafe?

Tags:

go

I was wondering if calling Write() on an os.File is thread safe. I'm having a hard time finding any mention of thread safety in the docs.

like image 248
Gabe Avatar asked Jun 10 '15 02:06

Gabe


People also ask

Is write thread safe?

write() is certainly thread-safe. The problem is that a partial write() could require multiple calls in order to completely write the data, and while that is "thread-safe" it could result in interleaved data.

Is Python file write thread safe?

Writing to File is Not Thread-Safe A best practice is to open a file using the context manager interface so that the file is closed automatically once any writing operations are finished. Writing to the same file from multiple threads concurrently is not thread safe and may result in a race condition.

Is Linux read thread safe?

Any system level (syscall) file descriptor access is thread safe in all mainstream UNIX-like OSes.

Is Fopen thread safe?

fopen(), fdopen() and freopen() are thread-safe. These interfaces are not async-cancel-safe.


1 Answers

The convention (at least for the standard library) is the following: No function/method is safe for concurrent use unless explicitly stated (or obvious from the context).

It is not safe to write concurrently to an os.File via Write() without external synchronization.

like image 148
Volker Avatar answered Sep 29 '22 08:09

Volker