473,587 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is cout << msg an atomic action?



void foo (int n)
{
std::ostringstr eam 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
Feb 4 '08 #1
5 2454

"Alex Vinokur" <al****@users.s ourceforge.netw rote in message
news:a8******** *************** ***********@1g2 000hsl.googlegr oups.com...
>

void foo (int n)
{
std::ostringstr eam 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....37f881199c5133
(read all...)

Feb 4 '08 #2
On Feb 4, 6:17 am, Alex Vinokur <ale...@users.s ourceforge.netw rote:
void foo (int n)
{
std::ostringstr eam 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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 4 '08 #3
On Feb 4, 11:42*am, James Kanze <james.ka...@gm ail.comwrote:
On Feb 4, 6:17 am,AlexVinokur< ale...@users.so urceforge.netwr ote:
void foo (int n)
{
* std::ostringstr eam 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?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Feb 4 '08 #4
"Alex Vinokur" <al****@users.s ourceforge.netw rote in message
news:38******** *************** ***********@q39 g2000hsf.google groups.com...
On Feb 4, 11:42 am, James Kanze <james.ka...@gm ail.comwrote:
On Feb 4, 6:17 am,AlexVinokur< ale...@users.so urceforge.netwr ote:
void foo (int n)
{
std::ostringstr eam 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.

Feb 5 '08 #5
On 4 Feb., 06:17, Alex Vinokur <ale...@users.s ourceforge.netw rote:
void foo (int n)
{
* std::ostringstr eam 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
Feb 5 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9014
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex' undeclared (first use this function)
4
5795
by: karolina | last post by:
Hi I am playing around with visual studio 7.0. Why does the code below do like it does? Where do I set the character set to use in cout, properties->configuration properties->General->character does not do the trick. #include <iostream> using namespace std;
4
2052
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the following will be printed A 3 2
4
1745
by: wangzhihuii | last post by:
Hi all, I'm really confused, can cout<<""; contribute anything to the routine ?!! my programm won't work properly without this trivial sentence. Sincerely vivian
12
6084
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
46
3375
by: suresh | last post by:
Hi, For this code snippet, I get the following output, which I am unable to understand. (2^31 = 2147483648) cout<< -2147483648 << endl; cout << numeric_limits<int>::min() <<',' << numeric_limits<int>::max()<< endl;
42
4505
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector of string, in one line. I could
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.