ofstream and logging & multiple threads

Hi All,

I'm new to this forum, but not new to C++. Doing this as of, I think 2005 or so, for a specific product, that still sells :)

So, I'm trying to minimize dependency on Win32 and ATL in my project.

Currently, I'm using CAtlFile with options append, and SYNCHRONIZE
(This is a TXT file)
HRESULT hr = m_file.Create(m_logFileName,
FILE_APPEND_DATA | SYNCHRONIZE,
FILE_SHARE_READ,
OPEN_ALWAYS);

This is in fact, Win32.
The 'fun' is, that multple threads can access the same file, it won't be garbled, just one thread may write and the others will WAIT.


But I wonder, is ofstream also safe for the same purpose?

So, Can I use ofstream, open a file for append, and multiple threads can 'try' to access the file, and append a line with thread safety?

Thanks!
I believe the parallel behavior of streams to be implementation-specified, at best.
Given code where two threads send "Hello World" to a stream without any kind of explicit synchronization, I've seen some implementations output "Hello WorldHello World" as long as it's done in a single operator>>() or write() call, and others output "HHeelllloo WWoorrlldd"

Your best bet is to just do the locking yourself.
I think you're right, thanks.

this confirms this as well

https://stackoverflow.com/questions/15513549/ofstream-shared-by-mutiple-threads-crashes-after-awhile

That would make me decide not to change the code. The Win32 API with SYNHRONIZE access flags, deals with this issue.
Last edited on
If you are a experienced C++ programmer that you say you are, this example will help you. The formatting on the website is pretty stupid, but it gives neat examples.

https://www.techrepublic.com/article/use-stl-streams-for-easy-c-plus-plus-thread-safe-logging/#

(also note I think this website is copy-pasted on like 10 different websites, so I dunno where it originates)

This code is pretty old, and I think I randomly picked the shortest version that works and used it. Not sure if the final version fixes the problem of forcing every line to end with an endl, or having a proper interrupt handling(which I had to add in myself), but heck it works.

There are also a dozen libraries that could do logging for you in a very robust manner as well, like boost's logger.
C++20 is just now getting a dedicated facility for this called std::osyncstream: https://en.cppreference.com/w/cpp/io/basic_osyncstream

until then, yes, there are very many logging libraries.
This is new to me. But I wonder how well this system works with cin and getline. In my implementation I made a drain and un-drain function so that any logs in the synced queue will be outputted, but new queued logs will not print unless un-drained.

I know in reality i know I should be calling a cheap OS/API dependant window to show critical errors, but I like how the terminal actually has a use other than being a fetus, (like specifying if the client wants to send a core dump / log to my server as a post-mortem).
Topic archived. No new replies allowed.