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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:42:18+00:00 2026-06-06T04:42:18+00:00

I got one implementation of read/write locks which is below. Notice that in the

  • 0

I got one implementation of read/write locks which is below. Notice that in the beginning of the functions, there is a pthread_mutex_lock call. If it uses pthread_mutex_lock once anyway, then what is the benefit of using read/write locks. How is it better than simply using pthread_mutex_lock?

int pthread_rwlock_rlock_np(pthread_rwlock_t *rwlock)
{
    pthread_mutex_lock(&(rwlock->mutex));
    rwlock->r_waiting++;
    while (rwlock->r_wait > 0)
    {
        pthread_cond_wait(&(rwlock->r_ok), &(rwlock->mutex));
    }
    rwlock->reading++;
    rwlock->r_waiting--;
    pthread_mutex_unlock(&(rwlock->mutex));
    return 0;
}

int pthread_rwlock_wlock_np(pthread_rwlock_t *rwlock)
{
    pthread_mutex_lock(&(rwlock->mutex));
    if(pthread_mutex_trylock(&(rwlock->w_lock)) == 0)
    {
        rwlock->r_wait = 1;
        rwlock->w_waiting++;
        while (rwlock->reading > 0)
        {
            pthread_cond_wait(&(rwlock->w_ok), &(rwlock->mutex));
        }
        rwlock->w_waiting--;
        pthread_mutex_unlock(&(rwlock->mutex));
        return 0;
    }
    else
    {
        rwlock->wu_waiting++;
        while (pthread_mutex_trylock(&(rwlock->w_lock)) != 0)
        {
            pthread_cond_wait(&(rwlock->w_unlock), &(rwlock->mutex));
        }
        rwlock->wu_waiting--;
        rwlock->r_wait = 1;
        rwlock->w_waiting++;
        while (rwlock->reading > 0)
        {
            pthread_cond_wait(&(rwlock->w_ok), &(rwlock->mutex));
        }
        rwlock->w_waiting--;
        pthread_mutex_unlock(&(rwlock->mutex));
        return 0;
    }
}
  • 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-06T04:42:19+00:00Added an answer on June 6, 2026 at 4:42 am

    The rwlock->mutex mutex is used to protect the state of the rwlock structure itself, not the state of whatever the reader/writer lock may be protecting in your target program. This mutex is held only during the time the lock is acquired or released. It is entered into just briefly, to avoid corrupting the state that is needed for “bookkeeping” of the reader/writer lock itself. In contrast, the reader/writer lock may be held for an extended period of time by the callers performing the actual reads and writes on the structure the lock protects.

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

Sidebar

Related Questions

I've got an interface and an implementation, both with [ProtoContract]. There is one property,
I've read a few posts on this, but there's still one thing that's not
i got one page from curl which contain <?xml version=1.0 encoding=UTF-8?> <ipinfo xmlns=http://data.quova.com/1 xmlns:xs=http://www.w3.org/2001/XMLSchema-instance>
I've got one head scratcher of a problem that I can't seem to find
I've got one really big .java class file that has a lot of members.
When using Console.Read(), the implementation seems to think that as soon as you push
in my implementation, I have an interface as: ICachingManager . I've got now one
I've got a string that I want to read line-by-line, but I also need
This shouldn't be that hard that one may think, if I got it right.
I've got a lot of objects, every one has some tags, which are stored

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.