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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:23:16+00:00 2026-06-02T15:23:16+00:00

I have a task scheduler implemented like this : private readonly List<Task> mTasks =

  • 0

I have a task scheduler implemented like this :

  private readonly List<Task> mTasks = new List<Task>();
  private readonly ManualResetEvent mNoTaskEvent = new ManualResetEvent(false);

  public void AddTask(Task task)
  {
     lock (mTasks)
     {
        mTasks.Add(task);
        mNoTaskEvent.Set();
     }
  }

  public void RemoveTask(Task task)
  {
     lock (mTasks)
     {
        mTasks.Remove(task);
        if (mTasks.Count == 0)
           mNoTaskEvent.Reset();
     }
  }

  void BackgroundThreadProc()
  {
     while (mRunning)
     {
        mNoTaskEvent.WaitOne();

        if (!mRunning) break;

        Task nextTask;

        lock (mTasks)
        {
           mTasks.Sort(...);
           nextTask = mTasks.First();
        }

        nextTask.Run();
     }
  }

mNoTaskEvent allows to block the background thread when there is no task available.
There is a race condition, if another thread remove all remaining tasks between “mNoTaskEvent.WaitOne()” and “lock (mTasks)”.

How could I atomically acquire the mTasks lock when leaving mNoTaskEvent.WaitOne() ?

Edit
The pthread API has a function that does exactly what I need : pthread_cond_wait

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

    A simple fix for the race-condition would be to check the count again:

        lock (mTasks)
        {
           if (mTasks.Count < 1)
             continue;
    
           mTasks.Sort(...);
           nextTask = mTasks.First();
        }
    

    Using Monitor.Wait():

     while (mRunning)
     {
        //mNoTaskEvent.WaitOne();
    
        if (!mRunning) break;
    
        Task nextTask;
    
        lock (mTasks)
        {
            while (mTasks.Count < 1)
            {
               Monitor.Wait(mTasks);
    
               if (!mRunning) break;                
            }
    
            ...     
        }
    
    
    
    public void AddTask(Task task)
    {
      lock (mTasks)
      {
         mTasks.Add(task);
         //mNoTaskEvent.Set();
         Monitor.Pulse(mTasks);
      }
    }
    

    You would also need to Pulse() when stopping the whole chain.

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

Sidebar

Related Questions

I am new this sharepoint development and i have task in hand to do
I have this task for the project with 4 nested subprojects using Maven: For
I have created a scheduler class called scheduledInsert. The scheduler Job for this class
I have a simple console application which runs daily (called by windows task scheduler)
I have my own ITrigger . Basically, it looks like the below: public interface
i am working on a task scheduler applications as my college project, i have
I have a strange problem with task scheduler. Here are three tested configurations of
I have a scheduled job configured in an XML file: <task:scheduled-tasks scheduler=taskScheduler> <task:scheduled ref=scheduledJobs
I have a background task that I run using the ScheduledThreadPoolExecutor with code like
I have a task scheduler which runs a C# console application every minute. It

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.