[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fastest way for writing in text files in a MultiThreading application

Posted on 2003-11-13
3
Medium Priority
?
760 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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

872 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