Read-write locks permit concurrent reads and exclusive writes to a protected shared resource. The read-write lock is a single entity that can be locked in read or write mode. To modify a resource, a thread must first acquire the exclusive write lock. An exclusive write lock is not permitted until all read locks have been released.
Database access can be synchronized with a read-write lock. Read-write locks support concurrent reads of database records because the read operation does not change the record's information. When the database is to be updated, the write operation must acquire an exclusive write lock.
To change the default read-write lock attributes, you can declare and initialize an attribute object. Often, the read-write lock attributes are set up in one place at the beginning of the application so they can be located quickly and modified easily. The following table lists the functions discussed in this section that manipulate read-write lock attributes.
See Similar Synchronization Functions—Read-Write Locks for the Solaris threads implementation of read-write locks.
Table 4–8 Routines for Read-Write Lock Attributes
Operation |
Destination Discussion |
|
---|---|---|
Initialize a read-write lock attribute | ||
Destroy a read-write lock attribute | ||
Set a read-write lock attribute | ||
Get a read-write lock attribute |
#include <pthread.h> int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
pthread_rwlockattr_init(3THR) initializes a read-write lock attributes object attr with the default value for all of the attributes defined by the implementation.
Results are undefined if pthread_rwlockattr_init is called specifying an already initialized read-write lock attributes object. After a read-write lock attributes object has been used to initialize one or more read-write locks, any function affecting the attributes object (including destruction) does not affect any previously initialized read-write locks.
If successful, pthread_rwlockattr_init() returns zero. Otherwise, an error number is returned to indicate the error.
#include <pthread.h> int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
pthread_rwlockattr_destroy(3THR) destroys a read-write lock attributes object. The effect of subsequent use of the object is undefined until the object is re-initialized by another call to pthread_rwlockattr_init() An implementation can cause pthread_rwlockattr_destroy() to set the object referenced by attr to an invalid value.
If successful, pthread_rwlockattr_destroy() returns zero. Otherwise, an error number is returned to indicate the error.
#include <pthread.h> int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
pthread_rwlockattr_setpshared(3THR) sets the process-shared read-write lock attribute.
PTHREAD_PROCESS_SHARED
Permits a read-write lock to be operated on by any thread that has access to the memory where the read-write lock is allocated, even if the read-write lock is allocated in memory that is shared by multiple processes.
PTHREAD_PROCESS_PRIVATE
The read-write lock will only be operated upon by threads created within the same process as the thread that initialized the read-write lock; if threads of differing processes attempt to operate on such a read-write lock, the behavior is undefined. The default value of the process-shared attribute is PTHREAD_PROCESS_PRIVATE.
If successful, pthread_rwlockattr_setpshared() returns zero. Otherwise, an error number is returned to indicate the error.
#include <pthread.h> int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared);
pthread_rwlockattr_getpshared(3THR) gets the process-shared read-write lock attribute.
pthread_rwlockattr_getpshared() obtains the value of the process-shared attribute from the initialized attributes object referenced by attr.
If successful, pthread_rwlockattr_getpshared() returns zero. Otherwise, an error number is returned to indicate the error.