Uninterleaved String Output Streaming

ISO/IEC JTC1 SC22 WG21 N3665 - 2013-04-19

Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org

Introduction

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.

Solution

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.

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

To make implementation reasonable, we restrict the size of the string to less than BUFSIZ.

Wording

This wording is relative to N3485.

27.4.1 Overview [iostream.objects.overview]

Edit paragraph 4 as follows.

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). Furthermore, for these iostream objects, when a string output operation is given a string of length less than BUFSIZ (27.9.2 [c.files]) bytes, that string shall appear in the output character sequence with no other characters interleaved within it. [Note: Users must still synchronize other concurrent use of these objects and streams by multiple threads if they wish to avoid interleaved characters. —end note]