Is cout << msg an atomic action?

J

James Kanze

void foo (int n)
{
std::eek:stringstream oss;
oss << "ABCD: " << n << std::endl;
std::cout << oss.str() << std::flush;

}
That function has been invoked in multiprocessing mode.
Output was something like:
ABCA: B1
C
etc.
Is cout << msg an atomic action?

It depends on the implementation, but I would not normally
expect to be able to write to the same stream from two different
threads without some external synchronization. Both reading
from and writing to an iostream modify the object itself.
 
A

Alex Vinokur

It depends on the implementation, but I would not normally
expect to be able to write to the same stream from two different
threads without some external synchronization.  Both reading
from and writing to an iostream modify the object itself.

I realize that
cout << msg1 << msg2;
is not an atomic action.

But is
cout << msg1;
also not atomic one?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
J

James Kanze

I realize that
cout << msg1 << msg2;
is not an atomic action.
But is
cout << msg1;
also not atomic one?

Define first what you mean by "atomic".

In general, it "modifies" cout. So if any other thread accesses
cout in any way, you need external synchronization of some sort,
at least if the library implements the Posix rules (which as far
as I can tell, also hold under Windows). But it very definitely
depends on the library.

The Rogue Wave library used with Sun CC does give more
guarantees, for example. Nothing useful, of course, but enough
to result in slowing down output by an order of magnitude.
Although not relevant in this example, the g++ library formally
gives less guarantee: as documented, you cannot even call
cout.good() in two different threads without external
synchronization (although in practice, the only time you'll run
into any problems is with string, and then only in a very few
special cases).

The current standard doesn't say anything about threads, so you
can't ask it. The next version will, however, and according to
all likelyhood, will offer the standard Posix guarantee (and no
more).
 
P

peter koch

void foo (int n)
{
  std::eek:stringstream oss;
  oss << "ABCD: " << n << std::endl;
  std::cout << oss.str() << std::flush;

}

That function has been invoked in multiprocessing mode.

Output was something like:
ABCA: B1
C
etc.

Is cout << msg an atomic action?

I would be most surprised if it was. In theory, an implementation
could make some of these operations atomic, but it would be quite a
stupid thing to do for the simple reason that the atomicity is at the
wrong level.
In case you have user provided operator << these will hardly be atomic
as they can't use the same syncronisation objects as std::cout.

/Peter

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top