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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:22:51+00:00 2026-06-17T15:22:51+00:00

I have a specific locking order that I am following. I am writing a

  • 0

I have a specific locking order that I am following. I am writing a thread pool to do some tasks that are currently done sequentially.

The locking order is pool->queue->job.

However, sometimes I need to lock the job to check the state of the job, and then lock the queue to move the job from one queue to another. So, to follow the lock ordering I get this:

lock job

if (job->state == CANCELED) {
unlock job
lock queue
lock job
// check that it is still canceled and do work
}

My question is, is there an alternate way to do this that doesn’t have the unlock/lock job? How is this handled when lock order must be preserved and a lock ‘higher up’ is needed?

  • 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-17T15:22:52+00:00Added an answer on June 17, 2026 at 3:22 pm

    I think you could do a a try-lock on queue. If it succeeds, you have both locks and can continue. If it fails, someone else has the queue lock already and as you don’t know if he’s maybe waiting for your job lock, you must do an unlock on job first before you may do a blocking lock on queue, otherwise you might deadlock.

    I think the following code won’t ever deadlock:

    lock(job);
    if (job->state == CANCELED) {
        if (!tryLock(queue)) {
            // Cannot lock queue; must avoid dead lock
            unlock(job);
            lock(queue); // This one might block now
            lock(job);
        }
        if (job->state == CANCELED) {
             // Do work
        }
        unlock(queue);
    }
    unlock(job);
    

    You might be unlocking queue and job in different order of locking, depending if try-lock succeeded or not (order is right if it succeeded, wrong otherwise), yet I still think it won’t ever deadlock.

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

Sidebar

Related Questions

In my rails 3.1 app, I have specific files that are to be loaded
I have a specific class C and I would like to overload some math
I have a specific requirement that all children of a particular JComponent have double
I'm looking to pull a specific line from a number of table that have
Let's say I have some system that coordinates the transfer of many files; that
I have a PHP/MySQL e-commerce site that is placing order detail information into a
I have a PictureBox on my form that shows img.jpg file inside a specific
In order to stop this being subjective, I have a couple of specific questions
I am almost done with an algorithm that processes some very large integers (around
I have a for loop that searches an array of structures in order 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.