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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:45:54+00:00 2026-05-15T17:45:54+00:00

I have asked this question before – but I have spent some time thinking

  • 0

I have asked this question before – but I have spent some time thinking about it and have implemented a working version.

Overview
1) Threads are being created to perform a certain task.
2) Only one thread can perform the task at a time.
3) Each thread performs the exact same task. (Does a bunch of checks and validations on a system)
3) The threads are being created faster than the task can be performed. (I have no control over the thread creation)

Result is that overtime I get a backlog of threads to perform the task.

What I have implemented goes as follows
1) Thread checks to see how many active threads there are.
2) If there are 0 threads it is marked to PerformTask and it starts the task
3) If there is 1 thread it is marked to PerformTak and it blocks
4) If there is more than 1 thread the thread is not marked to PerformTasks and just dies

The idea is that if there is a thread waiting to perform the task already I just kill the thread.

Here is the code that I came up with

              bool tvPerformTask = false;

              ivNumberOfProcessesSemaphore.WaitOne();
              if (ivNumberOfProcessesWaiting == 0 ||
                  ivNumberOfProcessesWaiting == 1)
              {
                ivNumberOfProcessesWaiting++;
                tvPerformTask = true;
              }
              ivNumberOfProcessesSemaphore.Release();

              if (tvPerformTask)
              {
                //Here we perform the work
                ivProcessSemaphore.WaitOne();
                //Thread save

                ivProcessSemaphore.Release();


                ivNumberOfProcessesSemaphore.WaitOne();
                ivNumberOfProcessesWaiting--;
                ivNumberOfProcessesSemaphore.Release();
              }
              else
              {
                //we just let the thread die
              }

The problem that I have is not that it doesn’t work it is just that I do not find the code elegant specifically I am not very happy that I need 2 semaphores an integer and a local flag to control it all. If there a way to implement this or pattern that would make the code simpler.

  • 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-15T17:45:55+00:00Added an answer on May 15, 2026 at 5:45 pm

    How about this?

    private readonly _lock = new object();
    private readonly _semaphore = new Semaphore(2, 2);
    
    private void DoWork()
    {
        if (_semaphore.WaitOne(0))
        {
            try
            {
                lock (_lock)
                {
                    // ...
                }
            }
            finally
            {
                _semaphore.Release();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I see that people have asked this question before, but I'm seeing some
This question has been asked before ( link ) but I have slightly different
Guys I have asked this question before but did not receive a single comment
This question may have been asked before, but I couldn't find an answer to
I know this question has been asked before but I have a design question
I know questions of this kind have been asked before, but my situation differs
I have asked a similar question before, but I didn't have a firm grasp
I have asked this question before and the problem was half solved in the
I have looked around here to see if somebody has asked this question before
Yes, I have already asked this question, but the problem is much more specific.

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.