Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Oct 2017 23:34:52 +0200
From:      Joerg Sonnenberger <joerg@bec.de>
To:        freebsd-hackers@freebsd.org
Subject:   Re: fprintf - threadsafe? - i.e. with process linked against '-pthread'?
Message-ID:  <20171011213452.GB32399@britannica.bec.de>
In-Reply-To: <03DA6274A199550235DC7351@[10.12.30.106]>
References:  <03DA6274A199550235DC7351@[10.12.30.106]>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Oct 11, 2017 at 10:13:35AM +0100, Karl Pielorz wrote:
> Presumably (and from what I can see) fprintf is 'thread safe'? - And it also
> appears multiple threads could write to a single file using it (i.e. it
> provides for atomic writes so lines won't intermingle - the lines written
> don't seem to intermingle).

stdio does not provide atomic IO. If you have two file handles or even
just two separate FILE instances sharing the same file handle and two
threads or processes are writing to them concurrently, buffering will
result in seemingly random interwoven output. Even using non-buffered IO
can trigger the same behavior as there is internal blocking going on.
Try a printf that created i.e. 16K output or so in one way, it should
increase the chance of seeing this a lot.

Joerg



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20171011213452.GB32399>