Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fastest way for writing in text files in a MultiThreading application

Posted on 2003-11-13
3
Medium Priority
?
759 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
Suggested Courses

636 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