Linux thread read/write lock

Source: Internet
Author: User
In Linux, the thread read/write lock has a write-first read/write lock, which has the following features:
1) Multiple readers can read at the same time
2) The writer must be mutually exclusive (only one writer is allowed to write, nor can the reader writer simultaneously)
3) The writer takes precedence over the reader (if a writer exists, the reader will have to wait and the writer will be given priority when waking up)

The read/write locks are provided directly in Solaris, but only the read/write locks of threads are provided in Linux. Here we record some data about the read/write locks.

1. Solaris. vs. Linux POSIX library functions

Solaris Library (LIB thread) Linux POSIX Library (libp thread) Operation
Sema_destroy () Sem_destroy () Destroy the Signal status.
Sema_init () Sem_init () Initialize the signal.
Sema_post () Sem_post () Increase signal.
Sema_wait () Sem_wait () Blocked signal count.
Sema_trywait () Sem_trywait () Reduce the signal count.
Mutex_destroy () Pthread_mutex_destroy () Destroys or disables States related to mutex objects.
Mutex_init () Pthread_mutex_init () Initialize mutex variables.
Mutex_lock () Pthread_mutex_lock () Lock the mutex object and block until the mutex object is released.
Mutex_unlock () Pthread_mutex_unlock () Release mutex objects.
Cond_broadcast () Pthread_cond_broadcast () Unblocks all threads waiting for the condition variable.
Cond_destroy () Pthread_cond_destroy () Destroys any State related to the condition variable.
Cond_init () Pthread_cond_init () Initialize the condition variable.
Cond_signal () Pthread_cond_signal () Remove the blocking of the next thread of the waiting condition variable.
Cond_wait () Pthread_cond_wait () Block the condition variable and release it at the end.
Rwlock_init () Pthread_rwlock_init () Initialize read/write locks.
Rwlock_destroy () Pthread_rwlock_destroy () Lock read/write locks.
Rw_rdlock () Pthread_rwlock_rdlock () Read/write locks.
Rw_wrlock () Pthread_rwlock_wrlock () Write/Write lock.
Rw_unlock () Pthread_rwlock_unlock () Unlock read/write locks.
Rw_tryrdlock () Pthread_rwlock_tryrdlock () Read locks on non-blocking read/write locks.
Rw_trywrlock () Pthread_rwlock_trywrlock () Write locks on non-blocking read/write locks.



2. Use mutex to implement

Set three mutex semaphores:
Rwmutex is used to access Shared data that is mutually exclusive to other readers/writers.
Rmutex is used to access the reader counter readcount that is mutually exclusive to the reader.
Nrmutex is used by the writer to wait for the reader to exit.

VaR rwmutex, rmutex, nrmutex: semaphore: = 1, 1;
Int readcount = 0;

Cobegin
Reader begin
P (rwmutex );
P (rmutex );
Readcount ++;
If (readcount = 1) P (nrmutex); // read/write operations are mutually exclusive.
V (rmutex );
V (rwmutex); // release the read/write mutex semaphores in a timely manner, allowing other read and write processes to apply for resource read data;

P (rmutex );
Readcount --;
If (readcount = 0) V (nrmutex); // all readers exit and allow write update
V (rmutex );
End

Writer begin
P (rwmutex); // mutually exclusive with other readers and writers
P (nrmutex); // If a reader is reading, wait for all readers to finish reading
Write update;
V (nrmutex); // allows the next new first reader to access and mutex write operations
V (rwmutex); // allows subsequent new readers and other writers
End
Coend

3. Use pthread_cond _ * & pthread_mutex _ * to implement rw_lock

# Include <pthread. h>
# Include <cstdlib>
# Include <ctime>
# Include <iostream>
Using namespace STD;

Class rwlock {

PRIVATE:
Pthread_mutex_t cnt_mutex;
Pthread_cond_t rw_cond;
Int rd_cnt, wr_cnt;

Rwlock (const rwlock &);
Rwlock & operator = (const rwlock &);

Public:
Rwlock (): rd_cnt (0), wr_cnt (0)
{
Pthread_mutex_init (& cnt_mutex, null );
Pthread_cond_init (& rw_cond, null );
}

Void get_shared_lock ()
{
Pthread_mutex_lock (& cnt_mutex );
While (wr_cnt> 0)
{
Pthread_cond_wait (& rw_cond, & cnt_mutex );
}
Rd_cnt ++;
Pthread_mutex_unlock (& cnt_mutex );
}

Void release_shared_lock ()
{
Pthread_mutex_lock (& cnt_mutex );
Rd_cnt --;
If (0 = rd_cnt)
{
Pthread_cond_signal (& rw_cond );
}
Pthread_mutex_unlock (& cnt_mutex );
}

Void get_exclusive_lock ()
{
Pthread_mutex_lock (& cnt_mutex );
While (rd_cnt + wr_cnt> 0)
{
Pthread_cond_wait (& rw_cond, & cnt_mutex );
}
Wr_cnt ++;
Pthread_mutex_unlock (& cnt_mutex );
}

Void release_exclusive_lock ()
{
Pthread_mutex_lock (& cnt_mutex );
Wr_cnt --;
Pthread_cond_broadcast (& rw_cond );
Pthread_mutex_unlock (& cnt_mutex );
}

~ Rwlock ()
{
Pthread_mutex_destroy (& cnt_mutex );
Pthread_cond_destroy (& rw_cond );
}
};

Class Test
{

PRIVATE:
Rwlock lock;

Static void * shared_task_handler (void * Arg)
{
Test * testptr = static_cast <test *> (ARG );

Testptr-> lock. get_shared_lock ();
// Do the shared task here
Testptr-> lock. release_shared_lock ();
}

Static void * exclusive_task_handler (void * Arg)
{
Test * testptr = static_cast <test *> (ARG );
Testptr-> lock. get_exclusive_lock ();
// Do the exclusive task here
Testptr-> lock. release_exclusive_lock ();
}

Public:
Typedef void * (* threadfunc) (void *);

Void start ()
{
Srand (Time (null ));

Const int threads_no = rand () %100;
Pthread_t * threads = new pthread_t [threads_no];

For (INT I = 0; I <threads_no; I ++)
{
Threadfunc tmpfunc = rand () % 2? Shared_task_handler: exclusive_task_handler;
If (pthread_create (threads + I, null, tmpfunc, this ))
{
Cerr <"pthread_create fails" <Endl;
Exit (1 );
}
}

For (INT I = 0; I <threads_no; I ++)
{
Pthread_join (threads [I], null );
}

Delete [] threads;
}
};

Int main ()
{
Test tmptest;
Tmptest. Start ();
}

--------------------------
Refer:
Solaris synchronous row-based failover and synchronization mechanism example
POSIX Thread Programming Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.