[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freebsd-hackers
Subject:    Re: fprintf - threadsafe? - i.e. with process linked against '-pthread'?
From:       Joerg Sonnenberger <joerg () bec ! de>
Date:       2017-10-11 23:03:16
Message-ID: 20171011230316.GA21395 () britannica ! bec ! de
[Download RAW message or body]

On Thu, Oct 12, 2017 at 12:54:17AM +0300, Konstantin Belousov wrote:
> On Wed, Oct 11, 2017 at 11:34:52PM +0200, Joerg Sonnenberger wrote:
> > 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.
> 
> Indeed stdio does not provide atomic IO, but it is thread-safe. That is,
> each isolated stdio operation, except the ones with explicit _unlocked
> suffix, are required to take an exclusive ownership on the FILE for the
> duration of the operation. See the description of flockfile().

That why I said "two separate FILE instances" :) The locking happens in
the FILE, not on the file descriptor.

Joerg
_______________________________________________
freebsd-hackers@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic