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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:22:43+00:00 2026-06-06T02:22:43+00:00

How are they implemented especially in case of pthreads. What pthread synchronization APIs do

  • 0

How are they implemented especially in case of pthreads. What pthread synchronization APIs do they use under the hood? A little bit of pseudocode would be appreciated.

  • 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-06T02:22:44+00:00Added an answer on June 6, 2026 at 2:22 am

    I haven’t done any pthreads programming for a while, but when I did, I never used POSIX read/write locks. The problem is that most of the time a mutex will suffice: ie. your critical section is small, and the region isn’t so performance critical that the double barrier is worth worrying about.

    In those cases where performance is an issue, normally using atomic operations (generally available as a compiler extension) are a better option (ie. the extra barrier is the problem, not the size of the critical section).

    By the time you eliminate all these cases, you are left with cases where you have specific performance/fairness/rw-bias requirements that require a true rw-lock; and that is when you discover that all the relevant performance/fairness parameters of POSIX rw-lock are undefined and implementation specific. At this point you are generally better off implementing your own so you can ensure the appropriate fairness/rw-bias requirements are met.

    The basic algorithm is to keep a count of how many of each are in the critical section, and if a thread isn’t allowed access yet, to shunt it off to an appropriate queue to wait. Most of your effort will be in implementing the appropriate fairness/bias between servicing the two queues.

    The following C-like pthreads-like pseudo-code illustrates what I’m trying to say.

    struct rwlock {
      mutex admin; // used to serialize access to other admin fields, NOT the critical section.
      int count; // threads in critical section +ve for readers, -ve for writers.
      fifoDequeue dequeue; // acts like a cond_var with fifo behaviour and both append and prepend operations.
      void *data; // represents the data covered by the critical section.
    }
    
    void read(struct rwlock *rw, void (*readAction)(void *)) {
      lock(rw->admin);
      if (rw->count < 0) {
        append(rw->dequeue, rw->admin);
      }
      while (rw->count < 0) {
        prepend(rw->dequeue, rw->admin); // Used to avoid starvation.
      }
      rw->count++;
      // Wake the new head of the dequeue, which may be a reader.
      // If it is a writer it will put itself back on the head of the queue and wait for us to exit.
      signal(rw->dequeue); 
      unlock(rw->admin);
    
      readAction(rw->data);
    
      lock(rw->admin);
      rw->count--;
      signal(rw->dequeue); // Wake the new head of the dequeue, which is probably a writer.
      unlock(rw->admin);
    }
    
    void write(struct rwlock *rw, void *(*writeAction)(void *)) {
      lock(rw->admin);
      if (rw->count != 0) {
        append(rw->dequeue, rw->admin);
      }
      while (rw->count != 0) {
        prepend(rw->dequeue, rw->admin);
      }
      rw->count--;
      // As we only allow one writer in at a time, we don't bother signaling here.
      unlock(rw->admin);
    
      // NOTE: This is the critical section, but it is not covered by the mutex!
      //       The critical section is rather, covered by the rw-lock itself.
      rw->data = writeAction(rw->data);
    
      lock(rw->admin);
      rw->count++;
      signal(rw->dequeue);
      unlock(rw->admin);
    }
    

    Something like the above code is a starting point for any rwlock implementation. Give some thought to the nature of your problem and replace the dequeue with the appropriate logic that determines which class of thread should be woken up next. It is common to allow a limited number/period of readers to leapfrog writers or visa versa depending on the application.

    Of course my general preference is to avoid rw-locks altogether; generally by using some combination of atomic operations, mutexes, STM, message-passing, and persistent data-structures. However there are times when what you really need is a rw-lock, and when you do it is useful to know how they work, so I hope this helped.

    EDIT – In response to the (very reasonable) question, where do I wait in the pseudo-code above:

    I have assumed that the dequeue implementation contains the wait, so that somewhere within append(dequeue, mutex) or prepend(dequeue, mutex) there is a block of code along the lines of:

    while(!readyToLeaveQueue()) {
      wait(dequeue->cond_var, mutex);
    }
    

    which was why I passed in the relevant mutex to the queue operations.

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

Sidebar

Related Questions

Subj. Are they implemented in trivial way with complexity O(N) or they are faster?
I've implemented a WebGrid. Sorting, paging and filtering do not work together. They work
Generally, final static members especially, variables (or static final of course, they can be
I have implemented a form of Steering behavior for a game, and per use
I have implemented a very basic sign up using email address+name, although I would
We all know what virtual functions are in C++, but how are they implemented
In the BubbleLevel example from apple they implement an +initialize method. They say: +initialize
...or, how do I filter a sequence of classes by the interfaces they implement?
Even if the class they reside in doesn't implement it?
I have to implement a few action. Each of they have some common elements,

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.