Article View: borland.public.cppbuilder.nativeapi
Article #13429Re: Multiple Threads & One Text File
From: Remy Lebeau (TeamB)
Date: Sun, 12 Oct 2003 07:21
Date: Sun, 12 Oct 2003 07:21
83 lines
3044 bytes
3044 bytes
"Josh Kirkpatrick" <jkirkpat@softhome.net> wrote in message
news:3f88f22a@newsgroups.borland.com...
> What would be the best method of ensuring that only one
> thread at a time writes to this text file and that the rest of
> the threads wait their turn?
Use a mutex or critical section to synchronize the access to the file.
Whenever a thread wants to access the file, it has to obtain a lock to the
mutex/cs first, and then release it when finished so that another thread can
obtain it later.
Even better, you can wrap the file access into a singleton class, that way
the file is only being accessed directly by a single object and only 1
instance of that object is allowed to be created. The object can then
handle the sychronizing internally. I use such an approach in several of my
projects, it works quite well. For example:
--- TLogFile.h ---
#ifndef TLogFileH
#define TLogFileH
class TLogFile
{
private:
CRITICAL_SECTION crit;
TLogFile();
public:
~TLogFile();
static TLogFile* Instance();
void Write(const AnsiString &Str);
};
#endif
--- TLogFile.cpp ---
#include "TLogFile.h"
TLogFile::TLogFile()
{
InitializeCriticalSection(&crit);
}
TLogFile::~TLogFile()
{
DeleteCriticalSection(&crit);
}
TLogFile* TLogFile::Instance()
{
static TLogFile log;
return &log;
}
void TLogFile::Write(const AnsiString &Str)
{
EnterCriticalSection(&crit);
try
{
// open/create the log file
// write the Str to the file
// close the file
}
__finally {
LeaveCriticalSection(&crit);
}
}
Then the calling threads can do the following when needed:
#include "TLogFile.h"
TLogFile::Instance()->Write("write something to the log");
...
TLogFile::Instance()->Write("write something else to the log");
...
Gambit
Technical Details
Message-ID:
<3f890078$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3f88f22a@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,2cf03e35cbac1db7 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-10-12 00:33:57 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f88f22a@newsgroups.borland.com> Subject: Re: Multiple Threads & One Text File Date: Sun, 12 Oct 2003 00:21:13 -0700 Lines: 85 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.22.137.75 Message-ID: <3f890078$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1065943160 67.22.137.75 (12 Oct 2003 00:19:20 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail