comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Is Text_IO.Put_Line() thread-safe?
Date: Fri, 15 Jun 2012 17:27:14 -0400
Date: 2012-06-15T17:27:14-04:00	[thread overview]
Message-ID: <wccd350qn9p.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 4fdac8dc$0$9508$9b4e6d93@newsspool1.arcor-online.net

Georg Bauhaus <rm.dash-bauhaus@futureapps.de> writes:

> But surely I/O routines in general should  not be thread safe
> and thus have to drag in everything required to achieve thread
> safety?

Yes, I suppose I agree that the programmer should have the option
of thread-UNsafety.  But here, we're talking about writing to
standard output.  And debug output at that.  Surely there should
be a convenient way to do that without horsing around with extra
tasks and whatnot.  Surely that should be the default.

> The result is still likely not safe, after all, if one can duplicate
> file descriptors, or is unaware of others writing to the same file,
> such as a log file. Neither does the thread safety of single Text_IO
> calls make sequences of Text_IO calls behave atomically.  Assuming
> Num_IO.Put and New_Line to be thread safe,
>
>   Num_IO.Put (123);  -- (1)
>   New_Line;  -- (2)
>
> What has happened between (1) and (2)?

Right.  That's one of the things I don't like about this design.
You should be able to write your entire "message" in a single call
and (at least optionally) make that call atomic.  The "message" could
be multiple lines, which Text_IO doesn't support at all.

The C printf style seems more convenient to me.  You write a template,
and fill in the "blanks" with variable data, formatted.  Printf is
too complicated, and not type safe, but those things could be
fixed in a different language.  The basic idea of a fill-in-the-blanks
template is a good one.

In Ada, you can do:

    Put_Line("Count = " & Integer'Image(Count));

but that has problems.

> An atomic sequence in a dedicated I/O task looks just right to me.

Maybe, but whatever the mechanism, it ought to be provided to
the programmer by the language.

Anyway, it has the same problem as your example above -- multiple
invocations of the entry are not atomic.  There's no getting around
that -- as soon as you have multiple tasks writing to the same file,
you have to decide on the granularity of the atomicity.

> A program sprinkled with I/O calls because, well, they are
> thread safe, is a good predictor of maintenance nightmares,
> as it lacks modularity and separation of concerns.

I agree that it's usually a good idea to separate I/O from
other processing.

- Bob



  parent reply	other threads:[~2012-06-15 21:27 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-14 12:53 Is Text_IO.Put_Line() thread-safe? awdorrin
2012-06-14 13:49 ` Robert A Duff
2012-06-14 14:35   ` Adam Beneschan
2012-06-14 14:38   ` Dmitry A. Kazakov
2012-06-14 14:56     ` J-P. Rosen
2012-06-14 16:01       ` Dmitry A. Kazakov
2012-06-14 18:34       ` Robert A Duff
2012-06-21 19:01         ` Randy Brukardt
2012-06-14 18:29     ` Robert A Duff
2012-06-21 19:04       ` Randy Brukardt
2012-06-14 21:14     ` tmoran
2012-06-14 14:42   ` awdorrin
2012-06-14 18:24     ` Robert A Duff
2012-06-14 20:37       ` awdorrin
2012-06-14 21:37         ` Robert A Duff
2012-06-15  5:32           ` Georg Bauhaus
2012-06-15  7:22             ` Dmitry A. Kazakov
2012-06-15 21:32               ` Robert A Duff
2012-06-16  7:41                 ` Dmitry A. Kazakov
2012-06-15 21:27             ` Robert A Duff [this message]
2012-06-14 18:56 ` Jeffrey Carter
2012-06-14 20:50   ` awdorrin
2012-06-14 21:41     ` Robert A Duff
2012-06-15 12:39       ` awdorrin
2012-06-14 22:17     ` Jeffrey Carter
2012-06-14 22:40       ` Simon Wright
2012-06-14 23:35         ` Jeffrey Carter
2012-06-15  5:04           ` Simon Wright
2012-06-15  5:41             ` Jeffrey Carter
2012-06-21 19:20             ` Randy Brukardt
2012-06-16  2:00       ` BrianG
2012-06-16  6:04         ` J-P. Rosen
2012-06-16  6:49           ` Simon Wright
2012-06-16  7:58             ` Dmitry A. Kazakov
2012-06-16  8:03               ` Simon Wright
2012-06-16  8:14                 ` Dmitry A. Kazakov
2012-06-21 19:27               ` Randy Brukardt
2012-06-16  7:51           ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox