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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:31:28+00:00 2026-06-01T00:31:28+00:00

So, I am working on a scheduler in one of my classes. Basically, we

  • 0

So, I am working on a scheduler in one of my classes. Basically, we are pretending that only one thread can be executing at a time. We are supposed to use a semaphore class to allow these threads to block themselves to simulate the thread waiting for the CPU.

The problem is, the threads seem to block at the wrong time and execute at the wrong time. I was wondering if I am missing some conceptual understanding of a semaphore and how to implement it. I was wondering if I could get some feedback on my implementation. The instructor provided this header file which I have not modified in any way:

class Semaphore {
private:
  int             value;
  pthread_mutex_t m;
  pthread_cond_t  c;

public:

  /* -- CONSTRUCTOR/DESTRUCTOR */

  Semaphore(int _val);

  //~Semaphore();

  /* -- SEMAPHORE OPERATIONS */

  int P();
  int V();
};

This is my implementation using posix stuff:

Semaphore::Semaphore(int _val){
    value = _val;
    c = PTHREAD_COND_INITIALIZER;
    m = PTHREAD_MUTEX_INITIALIZER;
}

int Semaphore::P(){
    if(value <= 0){
        pthread_cond_wait(&c, &m);
    }
    value--;
}

int Semaphore::V(){
    value++;
    if(value > 0){
        pthread_cond_signal(&c);
    }
}
  • 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-01T00:31:29+00:00Added an answer on June 1, 2026 at 12:31 am

    You are neglecting to lock the mutex.

    Secondly, what you have here is a counting semaphore, not a binary semaphore. A binary semaphore has only two states, and so a bool variable is appropriate:

    class Semaphore {
    private:
      bool            signaled;   // <- changed
      pthread_mutex_t m;
      pthread_cond_t  c;
    
      void Lock() { pthread_mutex_lock(&m); }          // <- helper inlines added
      void Unlock() { pthread_mutex_unlock(&m); }
    public:
    
      /* -- CONSTRUCTOR/DESTRUCTOR */
    
      Semaphore(bool);
    
      //~Semaphore();
    
      /* -- SEMAPHORE OPERATIONS */
    
      void P();   // changed to void: you don't return anything
      void V();
    };
    

    Impl:

    // consider using C++ constructor initializer syntax.
    
    Semaphore::Semaphore(bool s){        // don't use leading underscores on identifiers
        signaled = s;
        c = PTHREAD_COND_INITIALIZER;    // Not sure you can use the initializers this way!
        m = PTHREAD_MUTEX_INITIALIZER;   // they are for static objects.
    
        // pthread_mutex_init(&m); // look, this is shorter!
    }
    
    void Semaphore::P(){
        Lock();              // added
        while (!signaled){   // this must be a loop, not if!
            pthread_cond_wait(&c, &m);
        }
        signaled = false;
        Unlock();
    }
    
    void Semaphore::V(){
        bool previously_signaled;
        Lock();
        previusly_signaled = signaled; 
        signaled = true;
        Unlock();  // always release the mutex before signaling
        if (!previously_signaled)
          pthread_cond_signal(&c); // this may be an expensive kernel op, so don't hold mutex
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with RX scheduler classes using the .Schedule(DateTimeOffset, Action>) stuff. Basically I've
Working with an undisclosed API, I found a function that can set the number
SO I'm working on a schedule app that uses a MySQL DB. One of
I can understand how one can write a program that uses multiple processes or
I'm working on a project that needs two databases - one for the logged
I have been working on a scheduler engine which reads schedule Jobs from a
A quartz scheduler is being used in an Application I am working on. A
I'm currently working on an application that allows people to schedule Shows for an
I am working on a website in ASP.NET where e-mails can be scheduled for
I'm working on a scheduling system in Java that sends out reminders based on

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.