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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:31:18+00:00 2026-06-13T17:31:18+00:00

Suppose I have a Queue of Tasks, and each Task have a locking object

  • 0

Suppose I have a Queue of Tasks, and each Task have a locking object (syncObject) which controls shared resource access, Queue can have multiple Task which share same instances of syncObject. And I have N concurrent threads that should dequeue Tasks and proccess them in queue order, this means acquire lock on syncObject in the order of queue.

Code explanation:

abstract class Task
{
    public readonly Object SyncObject = new Object();
}

Queue<Task> taskQueue = new Queue<Task>();
Object queueLock = new Object();

void TakeItemThreadedMethod()
{
    Task task;
    lock(queueLock) task = taskQueue.Dequeue();
    //Between this lines is my problem,
    //Other thread can dequeue next task and it may share same syncObject and
    //acquire lock on it before this thread, thought this task was first in queue
    lock(task.SyncObject)
    {
        //Do some work here
    }
}

How to start proccessing Tasks (acquire Task.SyncObject lock) that share the same SyncObject in the order they were in Queue.

  • 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-13T17:31:19+00:00Added an answer on June 13, 2026 at 5:31 pm

    It sounds like potentially your queue shouldn’t contain individual tasks – but queues of tasks, where each subqueue is “all the tasks which share a sync-lock”.

    Your processor would therefore:

    • Take a subqueue off the main queue
    • Dequeue the first task off the subqueue and process it
    • When it’s finished put the subqueue back at the end of the main queue (or anywhere, actually – work out how you want the scheduling to work)

    This will ensure that only one task per subqueue is ever executed at a time.

    You’ll probably need a map from lock to subqueue, so that anything creating work can add it to the right subqueue. You’d need to atomically work out when to remove a subqueue from the map (and not put it back on the main queue), assuming you require that functionality at all.

    EDIT: As an optimization for the above, you could put the subqueue itself into whatever you’re using as the shared sync lock. It could have a reference to either “the single task to next execute” or “a queue of tasks” – only creating the queue lazily. You’d then put the sync lock (which wouldn’t actually need to be used as a lock any more) on the queue, and each consumer would just ask it for the next task to execute. If only a single task is available, it’s returned (and the “next task” variable set to null). If there are multiple tasks available, the first is dequeued.

    When a producer adds a new task, either the “first task” variable is set to the task to execute if it was previously null, or a queue is created if there wasn’t a queue but was already a task, or the queue is just added to if one already existed. That solves the inefficiency of unnecessary queue creation.

    Again, the tricky part will be working out how to atomically throw away the shared resource lock – because you only want to do so after processing the last item, but equally you don’t want to miss a task because you happened to add it at the wrong time. It shouldn’t be too bad, but equally you’ll need to think about it carefully.

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

Sidebar

Related Questions

Suppose we have a shared queue (implemented using an array), which two threads can
How can I remove an arbitrary item from a priority queue. Suppose I have
Suppose we have some resource(a file on disk) in which we have to write
Suppose that I have a priority queue which removes elements in increasing order, and
Suppose I have a static method of my class that returns an object of
Suppose I have an application fed by a MQ queue. When the application receives
I have an issue here. Suppose you have a business periodical task, for instance
Suppose I have to ехеcute several CPU-bound tasks. If I have 4 CPUs, for
Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing
Suppose I have such a template class: template <class T> class Queue { public:

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.