Fastest way for writing in text files in a MultiThreading application

Posted on 2003-11-13
Last Modified: 2007-12-19

I have an application that logs information into a text file, there are multiple threads in the file logging information. Sometimes when the application starts a high load, some entries are not logged in the file, but I have tested that errors happens only in the logging, because the missed entries are recorded in a database, for example.

so, I'm looking for the fastest way to lock this file and save the information, by now I'm using CreateFile like this, without sharing for locking automatically :

      m_hFile = CreateFile

The file is created hourly, in the name of the file is the date including current hour.

Visual C++ 6.0
Windows 2000 AS

What is the better way for doing this? maybe with fopen, fprintf or fwrite, with mutexes or a critical section?  

thanks in advance

Question by:cero
LVL 86

Accepted Solution

jkr earned 200 total points
ID: 9742800
Simply protect write access to the log file with critical sections:

CRITICAL_SECTION g_csLogFile; // initialize it using 'InitializeCriticalSection()'

void Log ( const char* p) {

    EnterCriticalSection ( &g_csLogFile);

    // write...

    LeaveCriticalSection ( &g_csLogFile);

this is the 'least cost' and yet effective way to do that.

Expert Comment

ID: 10249148
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9742800}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Setting nameservers after res_init fails doing res_query 2 96
How to gracefully close the c++ 11 thread? 3 94
max float value 3 41
Error creating a new C++ project in ,net 20 34
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question