is << on filestreams a blocking operation?

M

monade

Hi,
i have a file stream
std::eek:fstream 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++.
 
M

mlimber

Hi,
i have a file stream
std::eek:fstream 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::eek:stringstream, 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
 
P

Peter Jansson

Hi,
i have a file stream
std::eek:fstream 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
 
M

monade

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::eek:fstream::eek:perator<<()
 
D

David Harmon

On 13 Dec 2005 13:32:35 -0800 in comp.lang.c++, (e-mail address removed) 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::eek:fstream::eek:perator<<()

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top