473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

is << on filestreams a blocking operation?

Hi,
i have a file stream
std::ofstream file
and some data
std::string data
to serialize in a performance sensitive code section:
file << data << '\n'
I know that the data will be flushed when the streambuffer is full and
that writing to hard disk takes some time. Is the last line a
potentially blocking operation and if so, how do I avoid this?
My STL implementation is from visual c++.

Dec 13 '05 #1
4 1637
mon...@gmail.com wrote:
Hi,
i have a file stream
std::ofstream file
and some data
std::string data
to serialize in a performance sensitive code section:
file << data << '\n'
I know that the data will be flushed when the streambuffer is full and
that writing to hard disk takes some time. Is the last line a
potentially blocking operation and if so, how do I avoid this?
My STL implementation is from visual c++.


You want to delay the actual writing to a file on the hard disk? You
could just stream the object to a std::ostringstream, which would exist
only in memory, until you're willing to write the file. Then you can
flush the fstream to make sure all the disk access is complete.

Cheers! --M

Dec 13 '05 #2
mo****@gmail.com wrote:
Hi,
i have a file stream
std::ofstream file
and some data
std::string data
to serialize in a performance sensitive code section:
file << data << '\n'
I know that the data will be flushed when the streambuffer is full and
that writing to hard disk takes some time. Is the last line a
potentially blocking operation and if so, how do I avoid this?
My STL implementation is from visual c++.


Hi,
Looking at section 27.8.1.4, item 8, in the standard then I would
*guess* that it depends on your implementation of "a_codecvt.out(...)".
Perhaps you can try the sensitive code on you particular problem?

Regards,
Peter Jansson
Dec 13 '05 #3
Thanks for your answer,
unfortunately there is no way for me to decide when its a good moment
to flush the stream. Its a data collection application from an extern
socket source with a high accuracy timer to log timestamps when data
arrives. This timer would be delayed while writing to disk in a
blocking mode. I could buffer everything in a string stream until
application shutdown, as you said, but its a lot of data, so I would
prefer to avoid such extensiv memory usage. It would be great if the OS
performs the actual writing in a asynchronous way, but I'm not sure
about that with WindowsXP and Visual C++. The Standard says nothing
about the behaviour of std::ofstream::operator<<()

Dec 13 '05 #4
On 13 Dec 2005 13:32:35 -0800 in comp.lang.c++, mo****@gmail.com wrote,
It would be great if the OS
performs the actual writing in a asynchronous way, but I'm not sure
about that with WindowsXP and Visual C++. The Standard says nothing
about the behaviour of std::ofstream::operator<<()


You got it!

The C++ standard says nothing about it. And, of course, as this is not
comp.os.ms-windows.programmer.* or microsoft.public.win32.programmer.*
then "we" here should not speculate what some particular platform does
outside the C++ standard.

Dec 13 '05 #5

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

Similar topics

1
by: Kashish | last post by:
Is file<<"Some string"<<endl is an atomic operation. where file is an ofstream object. If we output a string in ofstream and we provide endl after that,Can we make sure the whole string will be...
5
by: Jeff | last post by:
We are using .Net and the wsdl Utility to generate proxies to consume web services built using the BEA toolset. The data architects on the BEA side create XML schemas with various entities in...
12
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; }
1
by: onkar | last post by:
#include<stdio.h> int main(void){ printf("%d %d\n",32<<1,32<<0); printf("%d %d\n",32<<-1,32<<-0); <----------------------------------see here printf("%d %d\n",32>>1,32>>0); printf("%d...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.