使用读写锁pthread_rwlock_t未定义的问题

在使用pthread_rwlock_t及相关函数时遇到未定义的问题,通过在程序开头定义_XOPEN_SOURCE为500来解决这个问题。测试代码中,初始化读写锁时遇到了编译错误,加入宏定义后成功编译。

使用读写锁pthread_rwlock_t未定义的问题

 

今天在程序里使用读写锁包括以下函数:

#include <pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t *rwptr);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwptr);
int pthread_rwlock_unlock(pthread_rwlock_t *rwptr);


int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwptr);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwptr);


int pthread_rwlock_init(pthread_rwlock_t *rwptr,
                     const pthread_rwlockattr_t *attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwptr);

int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
int pthread_rwlockattr_destroy(pthread_rwlockattr_t * attr);

int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * attr, int *valptr);

int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* attr, int value);
                 
都返回:成功 0,出错为正的Exxxx



测试程序如下:

#include <pthread.h>

pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;

int main()

{

       return 0;

}

 

$ gcc –o lock lock.c
$ lock.c:2: parse error before `lock'

$ lock.c:2: `PTHREAD_RWLOCK_INITIALIZER' undeclared here (not in a function)

$ lock.c:2: warning: data definition has no type or storage class

 

最后查文档解决方是:
在程序开头加入:
#define _XOPEN_SOURCE 500

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值