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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:33:41+00:00 2026-06-06T07:33:41+00:00

I have a section of code which should be executed by a maximum number

  • 0

I have a section of code which should be executed by a maximum number of threads lower than N and also the order in which threads are calling someFunction() should be reflected in the order in which they are entering the section, that is FIFO order.

If I use the Semaphore I have no control over the order in which threads are entering the section.

“There is no guaranteed order, such as FIFO or LIFO, in which blocked
threads enter the semaphore.”

The initial attempt:

class someClass
{
    static volatile Semaphore semaphore;
    ...

    someClass()
    {
        semaphore = new Semaphore(N,N)
    }

    someType someFunction(InputType input)
    {
        try
        {
            semaphore.WaitOne();
            /* Section Begins */
             var response = someHeavyJob(input); // submitted to the server
            return response;
            /* Section Ends */
        }
        finally
        {
            semaphore.Release();
        } 
    }
}

If I combine a Semaphore and a ConcurrentQueue as follows thread may come back with a response to the request brought by other thread what would require significant changes in other parts of code.
What is the .NET 4.5 solution for the following problem:

  1. Allow for maximum number of threads lower than N in the section of code
  2. The order in which threads are entering the section is FIFO
  3. Threads will get the response for the request they brought (and not the response to the request brought by other threads)

    class someClass
    {
        static volatile ConcurrentQueue<someType> cqueue;
        static volatile Semaphore semaphore;
        ...
    
        someClass()
        {
           cqueue = new ConcurrentQueue<someType>();
           semaphore = new Semaphore(N,N)
        }
    
        someType someFunction(Request request)
        {
            try
            {
                cqueue.enqueue(request);
                semaphore.WaitOne();
                Request newrequest;
                cqueue.TryDequeue(out newrequest);
    
                /* Section Begins */
    
                var response = someHeavyJob(Request newrequest); // submitted to the server
                return response;
                /* Section Ends */
            }
            finally
            {
                semaphore.Release();
            }
        }
    }
    

UPDATE:
I am clarifying my question:
SomeHeavyJobs() funciton is a blocking call to the server on which this job is being processed.

UPDATE2:
Thank you all for answers. For the record: I ended up using the FIFO Semaphore

  • 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-06T07:33:43+00:00Added an answer on June 6, 2026 at 7:33 am

    ‘If I combine a Semaphore and a ConcurrentQueue as follows thread may come back with a response to the request brought by other thread what would require significant changes in other parts of code.’

    I hate to say it, but I would suggest ‘changes in other parts of code’, even though I don’t know how much ‘significance’ this would have.

    Typicaly, such a requirement is met as you suggested, by queueing messages that contain a reference to the originating class instance so that responses can be ‘returned’ to the object that requested them. If the originators are all descended from some ‘messagehandler’ class, that makes it easier on the thread that will call the function, (which should be a member of messagehandler). Once the thread/s have performed the function, they can call a ‘onCompletion’ method of the messagehandler. ‘onCompletion’ could either signal an event that the originator is waiting on, (synchronous), or queue something to a private P-C queue of the originator, (asynchronous).

    So, a BlockingCollection, one consumer thread and judicious use of C++/C# inheritance/polymorphism should do the job.

    Strangely, this is almost exactly what I am being forced into with my current embedded ARM project. The command-line interface thread used for config/debug/log is now so large that it needs a massive 600 words of stack, even in ‘Thumb, Optimize of size’ mode. It can no longer be permitted to call the SD filesystem directly and must now queue itself to the thread that runs the SD card, (which has the largest stack in the system to run FAT32), and wait on a semaphore for the SD thread to call its methods and signal the semaphore when done.

    This is the classic way of ensuring that the calls are made sequentially and will cetainly work. It’s basicaly a threadpool with only one thread.

    Like the other posters have written, any other approach is likely to be, err.. ‘brave’.

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

Sidebar

Related Questions

Is the C++ vtable only used for determining which section of code should be
I have a section of code that can be summarised as follows; void MyFunc()
I have the following section of code in an app that I am writing:
I have some VERY simple code to return the title for a section header:
So I have a UITableView which should loop through a single NSMutableArray and use
I have a div section in which I have an image whose onclick event
I have a class with a template parameter which should decide which of two
I have code which uses the StreamReader to read HTML from a file, then
I have some inherited code which stores SMTP server, username, password in the system.net/mailSettings/smtp
The following is a section of code which builds a list of IP addresses

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.