使用 rwlock_init(3C) 可以初始化 rwlp 所指向的读写锁并将锁的状态设置为未锁定。
#include <synch.h> (或 #include <thread.h>) int rwlock_init(rwlock_t *rwlp, int type, void * arg);
type 可以是以下值之一:
同一个读写锁不能同时由多个线程初始化。读写锁还可以通过在清零的内存中进行分配来初始化,在这种情况下假设 type 为 USYNC_THREAD。对于其他线程可能正在使用的读写锁,不得重新初始化。
对于 POSIX 线程,请参见pthread_rwlock_init 语法。
#include <thread.h> rwlock_t rwlp; int ret; /* to be used within this process only */ ret = rwlock_init(&rwlp, USYNC_THREAD, 0);
#include <thread.h> rwlock_t rwlp; int ret; /* to be used among all processes */ ret = rwlock_init(&rwlp, USYNC_PROCESS, 0);
rwlock_init() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下任一情况,该函数将失败并返回对应的值。
EINVAL
描述:参数无效。
EFAULT
描述:rwlp 或 arg 指向的地址非法。