Is cout << msg an atomic action?



 DEVELOP > c-Plus-Plus > Is cout << msg an atomic action?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Alex Vinokur"
Date: 03 Feb 2008 11:17:07 PM
Object: Is cout << msg an atomic action?
void foo (int n)
{
std::ostringstream 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?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
.

User: "Chris Thomasson"

Title: Re: Is cout << msg an atomic action? 03 Feb 2008 11:32:00 PM
"Alex Vinokur" <alexvn@users.sourceforge.net> wrote in message
news:a8a79321-9b9c-4cae-ad30-710f0be329c6@1g2000hsl.googlegroups.com...



void foo (int n)
{
std::ostringstream 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?

no:
http://groups.google.com/group/comp.programming.threads/browse_frm/thread/6b37f881199c5133
(read all...)
.

User: "peter koch"

Title: Re: Is cout << msg an atomic action? 05 Feb 2008 05:08:28 PM
On 4 Feb., 06:17, Alex Vinokur <ale...@users.sourceforge.net> wrote:

void foo (int n)
{
=A0 std::ostringstream oss;
=A0 oss << "ABCD: " << n << std::endl;
=A0 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
.

User: "James Kanze"

Title: Re: Is cout << msg an atomic action? 04 Feb 2008 03:42:10 AM
On Feb 4, 6:17 am, Alex Vinokur <ale...@users.sourceforge.net> wrote:

void foo (int n)
{
std::ostringstream 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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "Alex Vinokur"

Title: Re: Is cout << msg an atomic action? 04 Feb 2008 04:54:55 AM
On Feb 4, 11:42=A0am, James Kanze <james.ka...@gmail.com> wrote:

On Feb 4, 6:17 am,AlexVinokur<ale...@users.sourceforge.net> wrote:

void foo (int n)
{
=A0 std::ostringstream oss;
=A0 oss << "ABCD: " << n << std::endl;
=A0 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. =A0Both 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
.
User: "Chris Thomasson"

Title: Re: Is cout << msg an atomic action? 04 Feb 2008 11:03:30 PM

"Alex Vinokur" <alexvn@users.sourceforge.net> wrote in message
news:383ba77f-9e28-4d61-b1e8-079c02dc67dc@q39g2000hsf.googlegroups.com...

On Feb 4, 11:42 am, James Kanze <james.ka...@gmail.com> wrote:

On Feb 4, 6:17 am,AlexVinokur<ale...@users.sourceforge.net> wrote:

void foo (int n)
{
std::ostringstream 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.

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

On a single-threaded application yes; otherwise, well, good luck.
.

User: "James Kanze"

Title: Re: Is cout << msg an atomic action? 05 Feb 2008 07:28:28 AM
On Feb 4, 11:54 am, Alex Vinokur <ale...@users.sourceforge.net> wrote:

On Feb 4, 11:42 am, James Kanze <james.ka...@gmail.com> wrote:

On Feb 4, 6:17 am,AlexVinokur<ale...@users.sourceforge.net>
wrote:

void foo (int n)
{
std::ostringstream 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.

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).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER