Solved

Fastest way for writing in text files in a MultiThreading application

Posted on 2003-11-13
3
751 Views
Last Modified: 2007-12-19
Hi,

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
      (
      myFileName,
      GENERIC_WRITE,
      0,
      NULL,
      OPEN_ALWAYS,
      FILE_ATTRIBUTE_NORMAL,
      NULL
      );

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

Environment:
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

cero
0
Comment
Question by:cero
3 Comments
 
LVL 86

Accepted Solution

by:
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.
0
 
LVL 9

Expert Comment

by:tinchos
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now