Go to main content

Multithreaded Programming Guide

Exit Print View

Updated: March 2019
 
 

Read-Write Lock Attributes

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. Set up at the beginning of the application makes the attributes easier to locate and modify. The following table lists the functions discussed in this section that manipulate read-write lock attributes.

Table 9  Routines for Read-Write Lock Attributes
Operation
Related Function Description
Initialize a read-write lock attribute
Destroy a read-write lock attribute
Set a read-write lock attribute
Get a read-write lock attribute

Initializing a Read-Write Lock Attribute

pthread_rwlockattr_init(3C) initializes a read-write lock attributes object attr with the default value for all of the attributes defined by the implementation.

pthread_rwlockattr_init() Syntax

#include <pthread.h>

int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);

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 initializes one or more read-write locks, any function that affects the object, including destruction, does not affect previously initialized read-write locks.

pthread_rwlockattr_init() Return Values

If successful, pthread_rwlockattr_init() returns zero. Otherwise, an error number is returned to indicate the error.

ENOMEM

Description: Insufficient memory exists to initialize the read-write attributes object.

Destroying a Read-Write Lock Attribute

pthread_rwlockattr_destroy(3C) destroys a read-write lock attributes object.

pthread_rwlockattr_destroy() Syntax

#include <pthread.h>

int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);

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.

pthread_rwlockattr_destroy() Return Values

If successful, pthread_rwlockattr_destroy() returns zero. Otherwise, an error number is returned to indicate the error.

EINVAL

Description: The value specified by attr is invalid.

Setting a Read-Write Lock Attribute

pthread_rwlockattr_setpshared(3C) sets the process-shared read-write lock attribute.

pthread_rwlockattr_setpshared() Syntax

#include <pthread.h>
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);

The pshared lock attribute has one of the following values:

PTHREAD_PROCESS_SHARED

Description: 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. Operation on the read-write lock is permitted even if the lock is allocated in memory that is shared by multiple processes.

PTHREAD_PROCESS_PRIVATE

Description: The read-write lock is only 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.

pthread_rwlockattr_setpshared Return Values

If successful, pthread_rwlockattr_setpshared() returns zero. Otherwise, an error number is returned to indicate the error.

EINVAL

Description: The value specified by attr or pshared is invalid.

Getting a Read-Write Lock Attribute

pthread_rwlockattr_getpshared(3C) gets the process-shared read-write lock attribute.

pthread_rwlockattr_getpshared() Syntax

#include <pthread.h> 
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, 
          int *restrict pshared);

pthread_rwlockattr_getpshared() obtains the value of the process-shared attribute from the initialized attributes object referenced by attr.

pthread_rwlockattr_getpshared() Return Values

If successful, pthread_rwlockattr_getpshared() returns zero. Otherwise, an error number is returned to indicate the error.

EINVAL

Description: The value specified by attr or pshared is invalid.