Multi processes read&write one file

后端 未结 5 1303
暗喜
暗喜 2020-11-30 02:26

I have a txt file ABC.txt which will be read and wrote by multi processes. So when one process is reading from or writing to file ABC.txt, file ABC.txt must be locked so tha

相关标签:
5条回答
  • 2020-11-30 02:59

    A mutex in C# may be shared across multiple processes. Here is an example for multiple processes writing to a single file:

    using (var mutex = new Mutex(false, "Strand www.jakemdrew.com"))
    {
        mutex.WaitOne();
        File.AppendAllText(outputFilePath,theFileText);
        mutex.ReleaseMutex();
    }
    

    You need to make sure that the mutex is given a unique name that will be shared across the entire system.

    Additional reading here:

    http://www.albahari.com/threading/part2.aspx#_Mutex

    0 讨论(0)
  • 2020-11-30 03:05

    If it was me, I would install something like SQL Server Compact Edition for reading/writing this data.

    However, if you want to be able to lock access to a resource that is shared between multiple processes, you need to use a Mutex or a Semaphore.

    The Mutex class is a .Net wrapper around an OS Level locking mechanism.

    Overview of Synchronization Primitives

    0 讨论(0)
  • 2020-11-30 03:07

    C#'s named EventWaitHandle is the way to go here. Create an instance of wait handle in every process which wants to use that file and give it a name which is shared by all such processes.

    EventWaitHandle waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "SHARED_BY_ALL_PROCESSES");
    

    Then when accessing the file wait on waitHandle and when finished processing file, set it so the next process in the queue may access it.

    waitHandle.WaitOne();
    /* process file*/
    waitHandle.Set();
    

    When you name an event wait handle then that name is shared across all processes in the operating system. Therefore in order to avoid possibility of collisions, use a guid for name ("SHARED_BY_ALL_PROCESSES" above).

    0 讨论(0)
  • 2020-11-30 03:09

    C# has a built in lock and you can use it by creating a static object which all threads have acess to:

    private static object lockObject {get;set;}
    
    public static object LockObject {get{return lockObject ?? lockObject = new object();}}
    
    
    void YourMethod()
    {
       lock(LockObject)  // all other threads will wait for y
       {
          using(var lockFileStream = File.Open("Lock.txt", FileMode.Open, FileAccess.Read, FileShare.None))
          {
             //Do what you need with the file.  
          }
    
       }
    }
    

    This creates a Thread Safe approach to your problem.

    0 讨论(0)
  • 2020-11-30 03:13

    Your solution is error prone. You've basically implemented double-checked locking (http://en.wikipedia.org/wiki/Double-checked_locking) which can be very unsafe.

    A better solution would be to either introduce thread isolation, whereby only one thread ever accesses the file and does so by reading from a queue upon which requests to read or write are placed by other threads (and of course the queue is protected by mutually exclusive access by threads) or where the threads synchronize themselves either by synchronization devices (lock sections, mutices, whatever) or by using some other file access logic (for example, System.IO.FileShare came up in a few reponses here.)

    0 讨论(0)
提交回复
热议问题