Use read/write locks

Source: Internet
Author: User
Tags call back
Use read/write locks

After configuring the read/write lock attribute, You can initialize the read/write lock. Use the following functions to initialize or destroy the read/write lock, lock or unlock the read/write lock, or attempt to lock the read/write lock. The following table lists the functions discussed in this section for processing read/write locks.

Table 4-9 examples of read/write locks

Operation

Function Description

Initialize read/write locks

Pthread_rwlock_initSyntax

Read the lock in the read/write lock

Pthread_rwlock_rdlockSyntax

Read locks from non-blocking read/write locks

Pthread_rwlock_tryrdlockSyntax

Lock used to write a read/write lock

Pthread_rwlock_wrlockSyntax

Write locks in non-blocking read/write locks

Pthread_rwlock_trywrlockSyntax

Unlock read/write locks

Pthread_rwlock_unlockSyntax

Delete read/write locks

Pthread_rwlock_destroySyntax

 

Initialize read/write locks

You can use pthread_rwlock_init (3C) throughATTRInitialize the referenced PropertyRwlockThe referenced read/write lock.

Pthread_rwlock_initSyntax

#include <pthread.h>



int pthread_rwlock_init(pthread_rwlock_t *rwlock,

const pthread_rwlockattr_t *attr);



pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

IfATTRIsNullThe default read/write lock attribute is used, and its function is the same as the address of the object passing the default read/write lock attribute. After the read/write lock is initialized, the lock can be used for any number of times without reinitialization. After successful initialization, the read/write lock status changes to initialized and unlocked. If you callPthread_rwlock_init ()To specify the initialized read/write lock, the result is uncertain. If the read/write lock is not initialized before use, the result is uncertain. For Solaris threads, seeRwlock_initSyntax.

If the default read/write lock attribute appliesPthread_rwlock_initializerA macro can initialize a read/write lock that is statically allocated. Its function is to callPthread_rwlock_init ()And set the parametersATTRSpecifiedNullDynamic Initialization is equivalent. The difference is that error checks are not performed.

Pthread_rwlock_initReturn Value

If yes,Pthread_rwlock_init ()Zero is returned. Otherwise, the error code used to indicate the error is returned.

IfPthread_rwlock_init ()Failed, will not initializeRwlockAndRwlockThe content is uncertain.

Einval

Description:

ATTROrRwlockThe specified value is invalid.

Get the read lock in the read/write lock

Pthread_rwlock_rdlock (3C) can be usedRwlockThe referenced read/write lock applies the read lock.

Pthread_rwlock_rdlockSyntax

#include <pthread.h>



int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock );

If the writer does not hold a read lock and no writer is blocked Based on the lock, the calling thread will obtain the read lock. If the writer does not hold a read lock but multiple writers are waiting for the lock, it is uncertain whether the calling thread can obtain the lock. If a writer holds a read lock, the calling thread cannot obtain the lock. If the call thread does not obtain the read lock, it will be blocked. The call thread must obtain the lock beforePthread_rwlock_rdlock (). If the call thread holdsRwlockWrite lock in, the result is uncertain.

To avoid a shortage of writer resources, the writer priority can be higher than the reader in multiple implementations. For example, the priority of the writer in the Solaris thread implementation is higher than that of the reader. SeeRw_rdlockSyntax.

A thread canRwlockContains multiple concurrent read locks. This thread can be called successfully.Pthread_rwlock_rdlock () NTimes. This thread must callPthread_rwlock_unlock () NThe matching unlock operation can only be performed once.

If you call an uninitialized read/write lockPthread_rwlock_rdlock ()The result is uncertain.

The thread signal processing program can process the signal sent to the thread waiting for the read/write lock. After the return from the signal processing program, the thread will continue to wait for the read/write lock to execute the read, as if the thread is not interrupted.

Pthread_rwlock_rdlockReturn Value

If yes,Pthread_rwlock_rdlock ()Zero is returned. Otherwise, the error code used to indicate the error is returned.

Einval

Description:

ATTROrRwlockThe specified value is invalid.

Read locks from non-blocking read/write locks

Pthread_rwlock_tryrdlock (3C)Pthread_rwlock_rdlock ()Similarly, the difference is that if any thread holdsRwlockThe writer or writer in is based onRwlockBlocking, thenPthread_rwlock_tryrdlock ()The function fails. For Solaris threads, seeRw_tryrdlockSyntax.

Pthread_rwlock_tryrdlockSyntax

#include <pthread.h>



int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

Pthread_rwlock_tryrdlockReturn Value

If you obtainRwlockIf the read lock is executed in the referenced read/write lock objectPthread_rwlock_tryrdlock ()Zero is returned. If the lock is not obtained, an error code indicating the error is returned.

Ebusy

Description:

The writer cannot obtain the read/write lock to execute the read because the writer holds the lock or is blocked Based on the lock.

Lock used to write a read/write lock

Pthread_rwlock_wrlock (3C) can be usedRwlockThe referenced read/write lock.

Pthread_rwlock_wrlockSyntax

#include <pthread.h>



int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock );

If no other reader thread or writer thread holds the read/write lockRwlockThe Calling thread will obtain the write lock. Otherwise, the calling thread will be blocked. The call thread must obtain the lock beforePthread_rwlock_wrlock ()Call back. If the calling thread holds a read/write lock (read lock or write lock) during the call, the result is uncertain.

To avoid a shortage of writer resources, the writer priority can be higher than the reader in multiple implementations. (For example, the priority of the Solaris thread to allow the writer is higher than that of the reader. SeeRw_wrlockSyntax .)

If you call an uninitialized read/write lockPthread_rwlock_wrlock ()The result is uncertain.

The thread signal processing program can process the signal sent to the thread waiting for the read/write lock to execute the write. After the return from the signal processing program, the thread will continue to wait for the read/write lock to execute the write, as if the thread is not interrupted.

Pthread_rwlock_wrlockReturn Value

If you obtainRwlockWhen the read/write Lock Object is referencedPthread_rwlock_rwlock ()Zero is returned. If the lock is not obtained, an error code indicating the error is returned.

Write locks in non-blocking read/write locks

Pthread_rwlock_trywrlock (3C)Pthread_rwlock_wrlock ()Similarly, the difference is that if any thread currently holdsRwlock, ThenPthread_rwlock_trywrlock ()The function fails. For Solaris threads, seeRw_trywrlockSyntax.

Pthread_rwlock_trywrlockSyntax

#include <pthread.h>



int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

If you call an uninitialized read/write lockPthread_rwlock_trywrlock ()The result is uncertain.

The thread signal processing program can process the signal sent to the thread waiting for the read/write lock to execute the write. After the return from the signal processing program, the thread will continue to wait for the read/write lock to execute the write, as if the thread is not interrupted.

Pthread_rwlock_trywrlockReturn Value

If you obtainRwlockIf the referenced read/write lock object executes the write lockPthread_rwlock_trywrlock ()Zero is returned. Otherwise, the error code used to indicate the error is returned.

Ebusy

Description:

The read/write lock cannot be obtained for the write because it is locked for the read or write operation.

Unlock read/write locks

Pthread_rwlock_unlock (3C) can be released onRwlockThe lock held in the referenced read/write Lock Object.

Pthread_rwlock_unlockSyntax

#include <pthread.h>



int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);

If the call thread does not hold the read/write lockRwlockThe result is uncertain. For Solaris threads, seeRw_unlockSyntax.

If you callPthread_rwlock_unlock ()To release the read lock in the read/write lock object, and other read locks are currently held by the lock object, the object will remain read locked. IfPthread_rwlock_unlock ()If the last read lock of the call thread in the read/write Lock Object is released, the call thread is no longer the owner of the object. IfPthread_rwlock_unlock ()The last read lock of the read/write Lock Object is released.

If you callPthread_rwlock_unlock ()If the last write lock of the read/write Lock Object is released, the read/write Lock Object is in the unowned and unlocked state.

IfPthread_rwlock_unlock ()Unlock the read/write lock object, and multiple threads are waiting to get the object to execute the write, the scheduling policy can determine the thread that obtains the object to execute the write. If multiple threads are waiting for the read/write lock object to be read, the scheduling policy can determine the order in which the waiting thread obtains the object to execute the write. If multiple threads are based onRwlockThe reader and writer cannot determine who gets the lock first.

If you call an uninitialized read/write lockPthread_rwlock_unlock ()The result is uncertain.

Pthread_rwlock_unlockReturn Value

If yes,Pthread_rwlock_unlock ()Zero is returned. Otherwise, the error code used to indicate the error is returned.

Delete read/write locks

Pthread_rwlock_destroy (3C) can be destroyed.RwlockThe referenced read/write Lock Object and any resources used by the lock are released.

Pthread_rwlock_destroySyntax

#include <pthread.h>



int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);



pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

Call againPthread_rwlock_init ()Before the lock is reinitialized, the impact of the lock is uncertain. Implementation may causePthread_rwlock_destroy ()SetRwlockThe referenced object is set to an invalid value. If any thread holdsRwlockTime callPthread_rwlock_destroy ()The result is uncertain. Attempts to destroy uninitialized read/write locks may result in uncertain behavior. Read/write lock objects that have been destroyed can be usedPthread_rwlock_init ()To reinitialize. After the read/write Lock Object is destroyed, if the object is referenced in other ways, the result is uncertain. For Solaris threads, seeRwlock_destroySyntax.

Pthread_rwlock_destroyReturn Value

If yes,Pthread_rwlock_destroy ()Zero is returned. Otherwise, the error code used to indicate the error is returned.

Einval

Description:

ATTROrRwlockThe specified value is invalid.

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.