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

  • Home
  • SEARCH
  • 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 6321169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:06:14+00:00 2026-05-24T16:06:14+00:00

In standard C# synchronization classes (like Monitor), if several threads wait on some object

  • 0

In standard C# synchronization classes (like Monitor), if several threads wait on some object to be unlocked, no one can predict which thread will get the next lock.

I want to assign priority to all waiting threads, so they get locks according to this priority (if several threads wait for unlock).

How can I implement that?

  • 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-24T16:06:15+00:00Added an answer on May 24, 2026 at 4:06 pm

    The simplest approach, if there aren’t too many priority levels, would be to have a main lock plus an Int32 for each level except the highest indicating how many tasks of higher priority are waiting. A task that wants to acquire the lock would Interlocked.Increment the counter for each lower priority, then wait on the main lock and check to see whether any higher-priority tasks were waiting. If so, it would release and reacquire the main lock. Otherwise, Interlocked.Decrement the counters of lower-priority tasks, use the resource, and release the main lock. If the routine gives up on acquiring the lock, it should decrement the counters of lower-priority tasks.

    This approach would have some extra overhead from lower-priority tasks acquiring and releasing the master lock while higher-priority tasks wanted it, but it shouldn’t be too bad. Further, the order in which lower-priority tasks acquire a lock after a higher-priority task is done with it could be effectively random. Such issues could be avoided by adding a lock for each priority level, and having each task acquire the lock for its level after the Interlocked.Increment operations; as before, the Decrement should happen when the lock either acquires the resource or gives up on waiting for it.

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

Sidebar

Related Questions

Can standard pointers in .Net do this? Or does one need to resort to
So standard Agile philosophy would recommend making your domain classes simple POCOs which are
Standard signal, allows only one signal can be queued. Real Time signal allows more
In standard ASP.net applications ASP.net offered some protection from XSS attacks with validateRequest throwing
The standard naming convention in the Java world is to name packages, classes and
I have a preference that, when set, forces my application to perform some synchronization
i am using a xpathdocument to get some values by their xpath expression, which
I have the following situation: I have two threads thread1 , which is a
I am developing a Java app where several computers need to keep some a
Standard ML OCaml Haskell some other

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.