Linux uses read-write lock pthread_rwlock_t

Reprinted from: https://blog.csdn.net/onlyou930/article/details/6755593

Use read-write locks

After configuring the properties of the read-write lock, you can initialize the read-write lock. The following functions are used to initialize or destroy a read-write lock, lock or unlock a read-write lock, or attempt to lock a read-write lock. The following table lists the functions discussed in this section to handle read-write locks.

Table 4–9 Routines for Handling Read-Write Locks

操作

相关函数说明

Initialize read-write lock

pthread_rwlock_init  syntax

Read locks in read-write locks

pthread_rwlock_rdlock  syntax

Read locks in non-blocking read-write locks

pthread_rwlock_tryrdlock  syntax

Write to the lock in the read-write lock

pthread_rwlock_wrlock  syntax

Write to a lock in a non-blocking read-write lock

pthread_rwlock_trywrlock 语法

Unlock read-write lock

pthread_rwlock_unlock  syntax

Destroy the read-write lock

pthread_rwlock_destroy  syntax

Initialize read-write lock

Use  pthread_rwlock_init(3C) to  initialize   the read-write lock referenced by rwlock through the attribute referenced  by  attr .

pthread_rwlock_init  syntax

#include <pthread.h>



int pthread_rwlock_init(pthread_rwlock_t *rwlock,

        const pthread_rwlockattr_t *attr);



pthread_rwlock_t  rwlock = PTHREAD_RWLOCK_INITIALIZER;

If  attr  is  NULL , the default read-write lock attribute is used, which has the same effect as passing the address of the default read-write lock attribute object. After a read-write lock is initialized, the lock can be used any number of times without reinitialization. After successful initialization, the state of the read-write lock changes to Initialized and Unlocked. If called  pthread_rwlock_init() to specify an initialized read-write lock, the result is undefined. If the read-write lock is not initialized before use, the result is undefined. For Solaris threads, see rwlock_init  syntax .

If the default read-write lock attributes apply, the  PTHREAD_RWLOCK_INITIALIZER  macro initializes a statically allocated read-write lock, which is   equivalent to dynamic initialization by calling pthread_rwlock_init() and specifying the parameter  attr  as  NULL , except that no error checking is performed.

pthread_rwlock_init  return value

pthread_rwlock_init() Returns zero if successful . Otherwise, an error number indicating the error is returned.

If it  fails, rwlockpthread_rwlock_init()  will not be initialized  , and  the contents of rwlock  are indeterminate.

EINVAL

describe:

The value specified by attr  or  rwlock  is invalid.

Acquire the read lock in the read-write lock

pthread_rwlock_rdlock(3C) 可用来向 rwlock 所引用的读写锁应用读锁。

pthread_rwlock_rdlock 语法

#include <pthread.h>



int  pthread_rwlock_rdlock(pthread_rwlock_t *rwlock );

如果写入器未持有读锁,并且没有任何写入器基于该锁阻塞,则调用线程会获取读锁。如果写入器未持有读锁,但有多个写入器正在等待该锁时,调用线程是否能获取该锁是不确定的。如果某个写入器持有读锁,则调用线程无法获取该锁。如果调用线程未获取读锁,则它将阻塞。调用线程必须获取该锁之后,才能从 pthread_rwlock_rdlock() 返回。如果在进行调用时,调用线程持有 rwlock 中的写锁,则结果是不确定的。

为避免写入器资源匮乏,允许在多个实现中使写入器的优先级高于读取器。例如,Solaris 线程实现中写入器的优先级高于读取器。 请参见rw_rdlock 语法

一个线程可以在 rwlock 中持有多个并发的读锁,该线程可以成功调用 pthread_rwlock_rdlock() n 次。该线程必须调用 pthread_rwlock_unlock() n 次才能执行匹配的解除锁定操作。

如果针对未初始化的读写锁调用 pthread_rwlock_rdlock(),则结果是不确定的。

线程信号处理程序可以处理传送给等待读写锁的线程的信号。从信号处理程序返回后,线程将继续等待读写锁以执行读取,就好像线程未中断一样。

pthread_rwlock_rdlock 返回值

如果成功,pthread_rwlock_rdlock() 会返回零。否则,将返回用于指明错误的错误号。

EINVAL

描述:

attr 或 rwlock 指定的值无效。

读取非阻塞读写锁中的锁

pthread_rwlock_tryrdlock(3C) 应用读锁的方式与 pthread_rwlock_rdlock() 类似,区别在于如果任何线程持有 rwlock 中的写锁或者写入器基于 rwlock 阻塞,则 pthread_rwlock_tryrdlock() 函数会失败。对于 Solaris 线程,请参见rw_tryrdlock 语法

pthread_rwlock_tryrdlock 语法

#include <pthread.h>



int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

pthread_rwlock_tryrdlock 返回值

如果获取了用于在 rwlock 所引用的读写锁对象中执行读取的锁,则 pthread_rwlock_tryrdlock() 将返回零。如果没有获取该锁,则返回用于指明错误的错误号。

EBUSY

描述:

无法获取读写锁以执行读取,因为写入器持有该锁或者基于该锁已阻塞。

写入读写锁中的锁

pthread_rwlock_wrlock(3C) 可用来向 rwlock 所引用的读写锁应用写锁。

pthread_rwlock_wrlock 语法

#include <pthread.h>



int  pthread_rwlock_wrlock(pthread_rwlock_t *rwlock );

如果没有其他读取器线程或写入器线程持有读写锁 rwlock,则调用线程将获取写锁。否则,调用线程将阻塞。调用线程必须获取该锁之后,才能从 pthread_rwlock_wrlock() 调用返回。如果在进行调用时,调用线程持有读写锁(读锁或写锁),则结果是不确定的。

为避免写入器资源匮乏,允许在多个实现中使写入器的优先级高于读取器。(例如,Solaris 线程实现允许写入器的优先级高于读取器。请参见rw_wrlock 语法。)

如果针对未初始化的读写锁调用 pthread_rwlock_wrlock(),则结果是不确定的。

线程信号处理程序可以处理传送给等待读写锁以执行写入的线程的信号。从信号处理程序返回后,线程将继续等待读写锁以执行写入,就好像线程未中断一样。

pthread_rwlock_wrlock 返回值

如果获取了用于在 rwlock 所引用的读写锁对象中执行写入的锁,则 pthread_rwlock_rwlock() 将返回零。如果没有获取该锁,则返回用于指明错误的错误号。

写入非阻塞读写锁中的锁

pthread_rwlock_trywrlock(3C) 应用写锁的方式与 pthread_rwlock_wrlock() 类似,区别在于如果任何线程当前持有用于读取和写入的 rwlock,则 pthread_rwlock_trywrlock() 函数会失败。对于 Solaris 线程,请参见 rw_trywrlock 语法

pthread_rwlock_trywrlock 语法

#include <pthread.h>



int pthread_rwlock_trywrlock(pthread_rwlock_t  *rwlock);

如果针对未初始化的读写锁调用 pthread_rwlock_trywrlock(),则结果是不确定的。

线程信号处理程序可以处理传送给等待读写锁以执行写入的线程的信号。从信号处理程序返回后,线程将继续等待读写锁以执行写入,就好像线程未中断一样。

pthread_rwlock_trywrlock 返回值

如果获取了用于在 rwlock 引用的读写锁对象中执行写入的锁,则 pthread_rwlock_trywrlock() 将返回零。否则,将返回用于指明错误的错误号。

EBUSY

描述:

无法为写入获取读写锁,因为已为读取或写入锁定该读写锁。

解除锁定读写锁

pthread_rwlock_unlock(3C) 可用来释放在 rwlock 引用的读写锁对象中持有的锁。

pthread_rwlock_unlock 语法

#include <pthread.h>



int pthread_rwlock_unlock (pthread_rwlock_t  *rwlock);

如果调用线程未持有读写锁 rwlock,则结果是不确定的。对于 Solaris 线程,请参见rw_unlock 语法

如果通过调用 pthread_rwlock_unlock() 来释放读写锁对象中的读锁,并且其他读锁当前由该锁对象持有,则该对象会保持读取锁定状态。如果 pthread_rwlock_unlock() 释放了调用线程在该读写锁对象中的最后一个读锁,则调用线程不再是该对象的属主。如果 pthread_rwlock_unlock() 释放了该读写锁对象的最后一个读锁,则该读写锁对象将处于无属主、解除锁定状态。

如果通过调用 pthread_rwlock_unlock() 释放了该读写锁对象的最后一个写锁,则该读写锁对象将处于无属主、解除锁定状态。

如果 pthread_rwlock_unlock() 解除锁定该读写锁对象,并且多个线程正在等待获取该对象以执行写入,则通过调度策略可确定获取该对象以执行写入的线程。如果多个线程正在等待获取读写锁对象以执行读取,则通过调度策略可确定等待线程获取该对象以执行写入的顺序。如果多个线程基于 rwlock 中的读锁和写锁阻塞,则无法确定读取器和写入器谁先获得该锁。

如果针对未初始化的读写锁调用 pthread_rwlock_unlock(),则结果是不确定的。

pthread_rwlock_unlock 返回值

如果成功,pthread_rwlock_unlock() 会返回零。否则,将返回用于指明错误的错误号。

销毁读写锁

pthread_rwlock_destroy(3C) 可用来销毁 rwlock 引用的读写锁对象并释放该锁使用的任何资源。

pthread_rwlock_destroy 语法

#include <pthread.h>



int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);



pthread_rwlock_t  rwlock = PTHREAD_RWLOCK_INITIALIZER;

在再次调用 pthread_rwlock_init() 重新初始化该锁之前,使用该锁所产生的影响是不确定的。实现可能会导致 pthread_rwlock_destroy() 将 rwlock 所引用的对象设置为无效值。如果在任意线程持有 rwlock 时调用 pthread_rwlock_destroy(),则结果是不确定的。尝试销毁未初始化的读写锁会产生不确定的行为。已销毁的读写锁对象可以使用 pthread_rwlock_init() 来重新初始化。销毁读写锁对象之后,如果以其他方式引用该对象,则结果是不确定的。对于 Solaris 线程,请参见rwlock_destroy 语法

pthread_rwlock_destroy 返回值

如果成功,pthread_rwlock_destroy() 会返回零。否则,将返回用于指明错误的错误号。

EINVAL

描述:

attr 或 rwlock 指定的值无效。

转自:http://hi.baidu.com/andyzcj/blog/item/081b7018cf41aa4b43a9ad87.html


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325192180&siteId=291194637