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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:35:53+00:00 2026-05-19T23:35:53+00:00

I am looking for a type of lock where the thread that holds the

  • 0

I am looking for a type of lock where the thread that holds the lock can pass it on to another thread of its choosing.

Here is why I want it:

  • I have a class similar to ConcurrentHashMap – a specialized collection that is divided into multiple segments
  • Most modifications require only one segment to be locked. A few require two segments to be locked (specifically, modifying a key so that it moves from one segment to another.)
  • Most reads do not require locking – a volatile read is usually sufficient, but occasionally a search needs to lock all segments at once, if a modification-count check fails
  • Searching is done in multiple threads (via a ThreadPoolExecutor)
  • It is essential that the search function has a consistent view of all segments (e.g. it must not miss an entry while it is being moved from one segment to another.)
  • The search task (for a single segment) may be interrupted at any time.

Now I am considering the situation where the search method has been called in the main thread and discovers it needs to lock all segments. All of the segment locks must be held at once by the main thread (to ensure there is no interference) but it is not the main thread that will be doing the updates – it is one of the worker threads. Hence I am trying to make the main thread “pass on” the locks once it knows it has a consistent snapshot.

We used to use a single lock for the whole collection but as it got larger there was too much contention and unacceptably high latency for small updates.

Unlocking and re-locking (on a ReentrantLock) is not safe – another thread may modify the segment before the worker thread starts the search.

A plain Semaphore can handle locking and unlocking by different threads. The issue that then arises is who should release the semaphore – the worker thread needs a way to signal that it has taken ownership of the lock (because it may throw an exception before or after this point and the main thread needs to know whether to clean up.) It would also be tricky to unit-test because you never know if a semaphore acquire or release has occurred in the correct thread.

It would be a bonus if the lock could be used reentrantly in other methods (where it is not passed between threads.)

I imagine some combination of a Semaphore and an AtomicBoolean or AtomicReference is called for, but a quick google search did not reveal any examples. Is there any reason why this approach should not be used?

  • 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-19T23:35:54+00:00Added an answer on May 19, 2026 at 11:35 pm

    An inverted use of a read/write lock may work. In this idiom, the read lock holders perform concurrent writes to the data structure (e.g. independent slots in an array). The write lock is used to acquire exclusive access to perform a consistent read (e.g. summing the array). This is a rarely used idiom, but elegant in those oddball cases. Your problem is a bit hard to grok, but this may at least offer some inspiration for a concrete solution. I suspect with a deeper understanding, the problem could be simplified so that a classical solution is more appropriate.

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

Sidebar

Related Questions

Suppose I have a custom collection class that provides some internal thread synchronization. For
I have some flexibility here so I'm looking for some advice before I lock
Why is locking a type considered very bad? For example, lock(typeof(DateTime)) I understand that
I'm looking for some type of profiling utility I could use where I can
I'm looking to build a flyout type menu in Silverlight that will be added
I am looking for a Lua front-end compiler that is type-checked at compile time,
I have an object which is shared by multiple threads, I want to lock
Looking at the layout here , I want to constrain the entire page's min
I have a short lock guarded section in a method (that serves the request
I have a class that has two method in it, one calls a class

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.