Save the same file at the same time in different threads

Asked

Viewed 99 times

4

In my software I have a thread that every second updates the X file. This same file can also be updated through a user action that will be in another thread (may be at any time).

My question is: what will happen if both threads try to save X file at exactly the same time? There is no way to ensure that the file is updated in both cases?

I need to ensure that the file is updated in both cases. Failing to update the file is not an option.

I know it can be quite rare to happen (in the exact thousandth of a second) two threads try to save the same file at the same time. But what if it happens?

I am currently using the code below to save the files:

using (var streamWriter = new StreamWriter(filename, false))
{
    streamWriter.WriteLine(encrypted);
}
  • 1

    I think this one reply has something to do with the question

  • Thanks @Barbetta, I’ll read!

2 answers

4


The resource you want to access/manipulate (the file) is unique. So it is interesting that you can center operations on an object Singleton whatever thread safe. A model like this forces the creation of a FIFO queue to access the resource.

Note that threads awaiting access will enter in Wait-state - however this is a small price to pay to guarantee atomicity of operations.

For an example of thread-safe, read this article: Implementing the Singleton Pattern in C# (specifically the topics 'Second version - simple thread-Safety' and 'Fifth version - Fully Lazy instantiation').

The following codes are copies of the content of the article (to avoid possible broken links):

public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

Version via static startup:

public sealed class Singleton
{
    private Singleton()
    {
    }

    public static Singleton Instance { get { return Nested.instance; } }

    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

3

It’s not a direct answer that does what you want directly, but it’s the answer you should follow.

Treating competition is difficult, doing wrong is very easy, so it’s best to use another mechanism that already does this. I recommend using Sqlite.

That is, the two answers here are telling you to do something else :) Which will be more suitable for your case depends on the need that was not properly posted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.