<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">

<style type="text/css">

body {
  color: #000000;
  background-color: #FFFFFF;
}

del {
  text-decoration: line-through;
  color: #8B0040;
}
ins {
  text-decoration: underline;
  color: #005100;
}

p.example {
  margin: 2em;
}
pre.example {
  margin: 2em;
}
div.example {
  margin: 2em;
}

code.extract {
  background-color: #F5F6A2;
}
pre.extract {
  margin: 2em;
  background-color: #F5F6A2;
  border: 1px solid #E1E28E;
}

p.function {
}

p.attribute {
  text-indent: 3em;
}

blockquote.std {
  color: #000000;
  background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding: 0.5em;
}

blockquote.stddel {
  text-decoration: line-through;
  color: #000000;
  background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding: 0.5em;
}

blockquote.stdins {
  text-decoration: underline;
  color: #000000;
  background-color: #C8FFC8;
  border: 1px solid #B3EBB3;
  padding: 0.5em;
}

table {
  border: 1px solid black;
  border-spacing: 0px;
  margin-left: auto;
  margin-right: auto;
}
th {
  text-align: left;
  vertical-align: top;
  padding: 0.2em;
  border: none;
}
td {
  text-align: left;
  vertical-align: top;
  padding: 0.2em;
  border: none;
}

</style>

<title>Uninterleaved String Output Streaming</title>
</head>
<body>
<h1>Uninterleaved String Output Streaming</h1>

<p>
ISO/IEC JTC1 SC22 WG21 N3665 - 2013-04-19
</p>

<p>
Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org
</p>

<h2><a name="Introduction">Introduction</a></h2>

<p>
At present,
synchronized stream output operations
guarantee that they will not produce race conditions,
but do not guarantee that the resulting character sequence will be sensible.
Some form of synchronization is required.
Unfortunately, without a standard mechanism for synchronizing,
independently developed software will be unable to synchronize.
</p>

<h2><a name="Solution">Solution</a></h2>

<p>
We propose to require sufficiently small strings output to a stream
to appear as an uninterleaved whole in the output.
This is the minimum change necessary
to solve the most pressing problem.
</p>

<p>
The code generally required on the part of the programmer
to synchronize a sequence of stream operations to an <code>ostream</code>
is to
declare a local <code>stringstream</code>,
stream to that <code>stringstream</code>,
and when done,
stream the entire contents of the <code>stringstream</code>,
to the <code>ostream</code>.
</p>

<p>
To make implementation reasonable,
we restrict the size of the string to less than <code>BUFSIZ</code>.
</p>

<h2><a name="Wording">Wording</a></h2>

<p>
This wording is relative to N3485.
</p>

<h3><a name="iostream.objects.overview">27.4.1 Overview [iostream.objects.overview]</a></h3>

<p>
Edit paragraph 4 as follows.
</p>

<blockquote class="std">
<p>
Concurrent access to
a synchronized (27.5.3.4) standard iostream object's
formatted and unformatted input (27.7.2.1) and output (27.7.3.1) functions
or a standard C stream by multiple threads
shall not result in a data race (1.10).
<ins>Furthermore,
for these iostream objects,
when a string output operation
is given a string of length less than
<code>BUFSIZ</code> (27.9.2 [c.files]) bytes,
that string shall appear in the output character sequence
with no other characters interleaved within it.</ins>
[<i>Note:</i>
Users must still synchronize
<ins>other</ins> concurrent use of these objects and streams by multiple threads
if they wish to avoid interleaved characters.
&mdash;<i>end note</i>]
</p>
</blockquote>

</body>
</html>
