Solved

Fastest way for writing in text files in a MultiThreading application

Posted on 2003-11-13
3
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 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