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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:35:27+00:00 2026-06-05T05:35:27+00:00

If you want to lock methods on instances, you have to create a Mutex

  • 0

If you want to lock methods on instances, you have to create a Mutex for every instance and method:

class Foo
  def initialize
    @blue_mutex = Mutex.new
    @red_mutex = Mutex.new
  end

  def blue
    @blue || @blue_mutex.synchronize do
      @blue ||= Blue.new
    end
  end

  def red_mutex
    @red || @red_mutex.synchronize do
      @red ||= Red.new
    end
  end
end

Is there a logical error with having Thread.exclusive take arguments?

class Foo
  def blue
    @blue || Thread.exclusive("#{object_id}/blue") do
      @blue ||= Blue.new
    end
  end

  def red
    @red || Thread.exclusive("#{object_id}/red") do
      @red ||= Red.new
    end
  end
end

Why create mutexes if Thread.exclusive could just take an argument that defines the scope of the exclusivity?

  • 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-06-05T05:35:28+00:00Added an answer on June 5, 2026 at 5:35 am

    The short answer is that the API could be done the way you describe. In fact that is what Java does by letting you lock an arbitrary object. It has to play tricks because the tracking of the lock uses up some reserved space on the object itself. Also, when the object is locked and becomes contended, a real mutex has to be created, which uses some more space. But hopefully your code isn’t littered with objects being locked and unlocked. My guess is that the Ruby creators didn’t want to burden every object with this extra functionality that would only be used infrequently.

    I assumed above that when you said “clunky” you were referring to the readability of the code. If you are concerned about performance, I doubt there is much difference. Under the covers either approach can use the same, very efficient technique. It’s just a question of the extra memory to store the thin locks on each object. I found some interesting details about that here

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

Sidebar

Related Questions

I have a database and want to lock it ... I know I can
I have a UIScrollView with a UIView inside. I want to lock the x-axis
I have implemented ReaderWriterLockSlim, Now i don't want it to wait at the lock.
I seemingly have two choices: Make my class implement IDisposable . Create my DbCommand
I know when you want to lock method to be executed by only one
I have a class that might throw any run-time exceptions during initialization. I want
current situation: I have a service with several typical service methods like create(), remove(),
I want to call a few static methods of a CPP class defined in
I have this method: public bool Remove(EntityKeyType key) { lock (syncroot) { //wait if
I want to lock back button while my data process. I use AsyncTask to

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.