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 6248163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:00:24+00:00 2026-05-24T13:00:24+00:00

I am trying to find the best practice for sharing a lock file between

  • 0

I am trying to find the best practice for sharing a lock file between multiple classes. Before this I was putting multiple functions inside the same class, so they could share a mutual lock file and a producer/consumer queue. The code is getting a bit longer than I’d like, and I want to break them up into individual classes. When I do this I am unable to share the lock files, or the queues to add/remove the commands from.

I need to have a UI thread that accepts input from the user, and then places the commands into a locked queue that the worker threads can pull from. I would like to place the UI thread and the worker functions into different classes to keep my code nicely organized.

Sorry if this is too vague. Is there a way to do this that doesn’t break good design technique?

  • 1 1 Answer
  • 0 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-24T13:00:26+00:00Added an answer on May 24, 2026 at 1:00 pm

    If the locks are shared between multiple classes, and the “owner” is not readily identifiable, then place those locks into a class that is shared.

    public class MyLocks { ... all your locks ... }
    

    Then when you initialize your classes that utilize the locks, pass them into the constructor. Alternatively, you can have the classes instantiate MyLocks on their own, and then expose it as a property which you can then use in subsequent construction.

    ...
    var a = new ClassWithLocks();
    var b = new ClassWithLocks(a.MyLocks);
    var b = new SomeOtherClassWithLocks(a.MyLocks);
    ...
    

    The takeaway is to place the locks into their own unit.

    Update

    An additional benefit of placing multiple locks into their own class is that you can explicitly create methods for seizing and releasing locks. For example:

    void PushLock(string myToken, LOCK_ENUM theLockIWant);
    void PopLock(string myToken, LOCK_ENUM theLockIWant);
    

    Now a particular class or operation can request locks, but if they try to grab a lock whose enumerator is less than the lock they are requesting, you can throw an exception. This is important when you require multiple locks to perform an operation as you can ensure that the locks are seized and released in order, which is one of the most common culprits in deadlocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to find a best practice to load usercontrols using Ajax. My first
I am trying to find the best practice for generating and outputting html which
I'm trying to find out the best practice when removing characters from the start
Im trying to find best practices to write PHP. I just wonder is this
I am trying to find a best-practice to allow users to define the route
I'm trying to find out what is the best practice for naming and casing
I'm trying to find a design pattern or a best practice, or some other
I'm trying to find a best practice for getting values for a databound control.
Trying to find the best way of create an overlap/overlay layer to fill the
I'm trying to find the best way to speed up the delivery of the

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.