Re: 1 file, multiple threads

From: Jeff Shannon (jeff_at_ccvcorp.com)
Date: 11/27/04


Date: Fri, 26 Nov 2004 15:43:06 -0800

Jason wrote:

>>If I have multiple threads reading from the same file, would that be a
>>problem?
>>
>>
>
>As long as you open each file with 'r' or 'rb' access only, it is not
>a problem. I believe you can even write to that file from one (but
>only one) thread while reading the file in multiple other threads.
>
>

You *can*, but due to buffering issues, it's likely that the reader
threads will not see changes made by the writer thread properly, and may
have issues with separate disk-reads which nominally stop/start at the
same location not actually matching because the underlying disk file has
changed. (Note that disk reads, which go into a buffer, do not
necessarily correlate in any predictable way to calls to the read*()
family of functions.)

In order to ensure consistent access to a mutable (i.e. not read-only)
file from multiple threads, it would be necessary to ensure that only
one thread was accessing the file at a given instant (i.e. use some form
of locking/synchronizing mechanism), and to be careful to flush all
buffers both before and after any file access. If the file can change
at all, then the only time that a given thread can make *any*
assumptions about the state of the file is during a single section in
which that thread has exclusive access to the file.

Much simpler to designate a single file-handler 'server', and have each
thread access the file only through the intermediary of this server
(which could be implemented as a separate thread itself). The server
then manages all of the file buffers, both in and out, and can ensure
that each access happens in a consistent way.

Jeff Shannon
Technician/Programmer
Credit International



Relevant Pages

  • Re: Question about WMI connection and concurrent use
    ... Each one should act as an independent client to the proxy. ... It should run multiple threads - one ... WMI is multi-threaded and has an asynchronous ... >> My only server, which is allowed to make snmp queries [which I call the ...
    (microsoft.public.win32.programmer.wmi)
  • Re: IE doesnt preserve changes made by javascript when Back button is clicked
    ... Firefox seems fine. ... If this page forms part of a multiple form, ... The alternative is to actually use server side code to force every ... browser to be consistent by hard coding into the markup the (filtered ...
    (comp.lang.javascript)
  • Re: Certificate for SBS2003 for Multiple Public Domains REPOST
    ... I do remember ISA 2000 having multiple certificates. ... > on the network which is a dedicated web server. ... > SSL web site rules is the SBS/ISA box has to have a copy of each Cert ... >>> The current SBS certificate generated by CEICW contains 1 public DNS ...
    (microsoft.public.windows.server.sbs)
  • Re: downloading a single file using multiple threads
    ... The multi part requests in HTTP1.1 are meant to request a single part or multiple parts in a single request, but you can't requests multiple parts in parallel from multiple client threads. ... There's nothing about HTTP that requires servers to restrict their communications to a given client to a single connection, and there's nothing about HTTP that stipulates that an HTTP server needs to coordinate communications on independent connections. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Development for dual core machine
    ... several separate processes (web server, database server, Python ... anyway) the package should be able to use multiple CPUs. ... Python threading doesn't support multiple CPU's because of the GIL. ... For that matter, if your machine is just dual core, maybe it's ok to ...
    (comp.lang.python)