pythonic
pythonic

Reputation: 21665

Why read/write locks require a lock?

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;
    }
}

Upvotes: 2

Views: 1015

Answers (4)

jpalecek
jpalecek

Reputation: 47770

How is it better than simply using pthread_mutex_lock?

  1. Even with this implementation, the readers will run simultaneously in the critical section.
  2. There could be a better (less portable) implementation that would use atomics in the "fast path" (locking would still be needed when waiting).

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

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.

Upvotes: 4

Steve Jessop
Steve Jessop

Reputation: 279385

In both functions, rwlock->mutex is released before returning. That means that just because you hold an rwlock as reader or writer, doesn't mean you hold the mutex.

Half the point of a rwlock is that multiple readers can operate simultaneously, so that's the immediate advantage over just using a mutex. Those readers only hold the mutex briefly, in order to acquire the reader lock. They don't hold the mutex while they do their actual work.

Upvotes: 2

nhahtdh
nhahtdh

Reputation: 56829

It will allows multiple read OR a single write at a time, which is better than one read OR one write operation at a time.

Upvotes: 0

Related Questions