Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::ofstream threadsafe?

Started by Austrian Coder Oct 9, 2004 at 9:35 AM 2 replies 4.4k views
Original Post
Austrian Coder
Austrian Coder
Hi. I am working on a programm, which uses a std::ofstream based loger class. Now i need to know, if std::ofstream is threadsafe on windows and linux. Thanks, Christian
twanvl
twanvl
There are many ways a program can be thread safe, what do you mean exactly?
IIRC ostream is usually not thread safe, i.e.:
// thread astream << "test";// thread bstream << "test";

might output "tteesstt".
antareus
antareus
Don't assume that it is because the standard says nothing about thread-safety.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Evil Steve
Evil Steve
Wrap the part of your logger that writes to the file in a mutex. It shouldn't be much effort, just 4 calls (InitializeCriticalSection(), DeleteCriticalSection(), EnterCriticalSection(), LeaveCriticalSection())

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.