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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:10:14+00:00 2026-06-07T02:10:14+00:00

As a question of a job interview, what is the best&simple way to implement

  • 0

As a question of a job interview, what is the best&simple way to implement a semaphore using C#?
Please add your code
Thanks!
* Not using semphores with Win32 API, I mean To implement it yourself.

  • 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-07T02:10:15+00:00Added an answer on June 7, 2026 at 2:10 am

    The .NET framework comes with a Semaphore, so that’d be the easiest way to make one:

    var s = new Semaphore(2, 0);
    

    But I’m guessing the interviewer is more interested in the structure behind it…

    You would simply need a globally hosted, access controlled entity that external entities can call into to get and release locks. In C# you could do something similar by hosting a web service or remoting class that exposes such methods and can keep a count of active locks.

    When the lock limit is reached, you simply return a negative response, such as false to signify that the lock cannot be attained.

    Adapted from Parv Sharma in the comments. You might be looking at something like this. The semaphore needs to lock access to resources (as acquiring a semaphore slot is also a guarded action), but the resources themselves are what control the number of locks available. The application logic would then be dictating what exactly is being locked.

    class CustomSemaphore 
    {
        private static readonly object semaphoreLock = new object(); 
        private static readonly object[] resources = new[] { new object(), new object(), new object(), new object(), new object(), };
    
        public int AcquireLockObject() 
        { 
            lock (semaphoreLock)
            {
                for (int i = 0; i < 5; i++)
                { 
                    if (Monitor.TryEnter(resources[i]))
                        return i; 
                } 
            }
    
            return -1; // Lock unsuccessful.
        } 
    
        public void ReleaseLockObject(int lockIndex)
        {
            // Sanity check lockIndex range etc.
    
            lock (semaphoreLock)
            {
                Monitor.Exit(resources[lockIndex]);
            }
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's your opinion of the following job interview question? In the requirement it never
I was asked this question in a job interview, and I'd like to know
I have a job interview tomorrow and I'm trying to answer this question: There
Simple question, I think. I have org.springframework.scheduling.quartz.CronTriggerBean triggering one job once a day. Because
it is a interesting question. i am using Quartz.net to make job scheduler. also
This question was in my job interview.. I just to see whether I gave
I was asked this question recently during my job interview, and I couldn't answer
This is a question that I was asked on a job interview some time
I had an interesting job interview experience a while back. The question started really
i've attend the interview for a job, they asked me one question like how

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.