The efficiency of using pthread_rwlock when there are many readers

While I was looking at the pthread_rwlock_unlock function man page, I noticed that func will return EPERM if the calling thread does not have ownership of rwlock.

Because rdlock allows multiple threads to receive a lock, there must be a data structure, such as a reference or array, to hold the owner of one particular rwlock.

The question arises:

Rwlock is designed to achieve efficiency when a read operation is much more common than a write operation, but if there are a large number of different threads that have received a read lock, each time I call pthread_rwlock_unlock (), it takes time to find this means that the calling thread is a valid owner. what is the time complexity of this scenario.

Thanks a lot guys :)

+5
source share
2 answers

nm gave a good answer. Your assumption that the structures for ownership of lock rights are incorrect in the linux implementation that you noted and is similar to the count nm method affects.

Here is an edited version of the pthread_rwlock_t type from /usr/include/bits/pthreadtypes.h

  struct
  {
    int __lock;
    unsigned int __nr_readers;
    unsigned int __readers_wakeup;
    unsigned int __writer_wakeup;
    unsigned int __nr_readers_queued;
    unsigned int __nr_writers_queued;
    int __writer;
    int __shared;
    unsigned int __flags;
  } __data;

You can see the counter fields. In addition, pthread_rwlock_unlock.c does not return EPERM, and most of the work revolves around verifying the owner of the record in pthread_rwlock_wrlock.c . and pthread_rwlock_rdlock.c .

, .

, , , , , .

+6

, EPERM. - undefined, .

O (1), , . , , . . , LIFO- , .

+5

All Articles