🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13430

Re: Multiple Threads & One Text File

#13430
From: Rob
Date: Sun, 12 Oct 2003 07:04
31 lines
2376 bytes
"Josh Kirkpatrick" <jkirkpat@softhome.net> wrote in message
news:3f88f22a@newsgroups.borland.com...
> Hello.  I have (what I hope is) a simple question.
>
> I want to be able to have several different threads running
simultaneously,
> which will each perform a certain task and write its results to a common
log
> file.  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?
>

Have a function called WriteLogFile() that

a)   enters a critical section
b)   writes the data to the required file
c)   releases the critical section

The advantage of this is that the functions executing the threads don't need
to depend on details of how data is written to the log file.  They just do
the
function call, and it happens.   It is also not necessary for the caller to
do
explicit synchronisation in this case:  the threads shouldn't need to know,
nor
care, that other threads may be writing to the same file.

The trade-off is that this approach is more of a performance hit.  It is
necessary
to create the critical section (before the first call to WriteLogFile()).
Technical Details
Message-ID: <3f88feb7$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.icl.net!newsfeed.fjserv.net!news.algonet.se!algonet!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!news.cambrium.nl!news.cambrium.nl!news.cambrium.nl!news2.euro.net!216.166.71.118.MISMATCH!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
References: <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: "Rob" <nospam@nonexistant.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3f88f22a@newsgroups.borland.com>
Subject: Re: Multiple Threads & One Text File
Date: Sun, 12 Oct 2003 16:34:59 +0930
Lines: 34
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4922.1500
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4925.2800
NNTP-Posting-Host: 211.26.121.45
Message-ID: <3f88feb7$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1065942711 211.26.121.45 (12 Oct 2003 00:11:51 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.icl.net!newsfeed.fjserv.net!news.algonet.se!algonet!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!news.cambrium.nl!news.cambrium.nl!news.cambrium.nl!news2.euro.net!216.166.71.118.MISMATCH!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