Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:42+00:00 2026-05-17T17:12:42+00:00

What is the best way to atomically lock multiple files? One possibility I’ve thought

  • 0

What is the best way to atomically lock multiple files? One possibility I’ve thought of is a simple .lock file in the directory (I’m only protected from other instances of my program; I’m not necessarily trying to thwart other programs from disrespecting the lock.) However, everything I’ve learned about thread-safety screams “bad idea!” Would this be a safe way to go? Is trying to create a file atomic, so if I succeed in creating it, I know I have the lock? How should I go about this?

I’m writing my own bug tracker (mostly as an exercise, I know I could find good solutions out there,) which stores bugs in files on the network. I envision it kind of like SVN – a single directory gets taken over by the program, and used to hold bugs, revisions, screenshots, etc, all managed by the tracker. I’m assuming SVN has a way to ensure multiple clients don’t make commits at the exact same time, etc.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:12:43+00:00Added an answer on May 17, 2026 at 5:12 pm

    Open each file exclusively, if you find one that can’t be opened, then unlock the ones you’ve already locked. Always lock in alphabetical order to avoid deadlocks.

        private List<FileStream> OpenExclusive(List<string> filenames)
        {
            var streams = new List<FileStream>(filenames.Count);
    
            try
            {
                filenames.Sort();
                foreach (var name in filenames)
                {
                    streams.Add(File.Open(name, 
                      FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None));
                }
                return streams;
            }
            catch (Exception)
            {
                CloseAll(streams);
                return null;
            }
        }
    
        private void CloseAll(List<FileStream> streams)
        {
            if (streams == null) return;
            foreach (var stream in streams)
            {
                stream.Close();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what's the best way to read all my aspx files in a directory, search
What is the best way to manage multiple Amazon Web Services (AWS) accounts through
What is the best way to store lists (collections) of simple types in Google
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html
The best way I think to explain this is to tell you what my
The best way to explain my problem is by a example. I have a
Any best way to run the jasmine HTML reporter with browserify styled code? I
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.