Article View: borland.public.cppbuilder.nativeapi
Article #13387Re: Multiple Threads & One Text File
From: Dennis Jones
Date: Wed, 15 Oct 2003 04:14
Date: Wed, 15 Oct 2003 04:14
38 lines
2600 bytes
2600 bytes
"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> wrote in message news:3f890078$1@newsgroups.borland.com... > > "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: It's funny this thread was posted, because I need to do something similar, only I will have multiple PC's running the same application that need to synchonize access to a log file on a shared network drive. Any suggestions as to how that might be accomplished? At first I thought specifying the appropriate file sharing modes would work, but then I realized that that would only work to block access to the file for other processes running on the SAME machine. Now I think I am going to have to go with some sort of "access file" mechanism, something like what Paradox does with its paradox.net file. Any additional ideas would be welcome. - Dennis
Technical Details
Message-ID:
<3f8cc993$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3f88f22a@newsgroups.borland.com> <3f890078$1@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-14 21:21:01 PST From: "Dennis Jones" <djones@nospam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f88f22a@newsgroups.borland.com> <3f890078$1@newsgroups.borland.com> Subject: Re: Multiple Threads & One Text File Date: Tue, 14 Oct 2003 21:14:12 -0700 Lines: 40 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: 204.200.27.7 Message-ID: <3f8cc993$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1066191251 204.200.27.7 (14 Oct 2003 21:14:11 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail