By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

is << on filestreams a blocking operation?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.