pthread库中如何使用RECURSIVE_MUTEX

Keyser 2008-09-01 11:22:47
环境:SUSE 10.1

我一开始这样用:

#include <pthread.h>

int main()
{
pthread_mutex_t m = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
/* 失败,报告 PTHREAD_RECURSIVE_MUTEX_INITIALIZER 是未定义符号 */
/* 尝试 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,仍是未定义符号 */
}



后来尝试这样用:

#include <pthread.h>

int main()
{
pthread_mutex_t m;

pthread_mutexattr_t ma;
pthread_mutexattr_init(&ma);
pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE_NP); /* NP或非NP都尝试过 */

pthread_mutex_init(&m, &ma);

pthread_mutexattr_destroy(&ma);

/* 其他操作 */
}


能够通过编译,但gcc报告一个警告 pthread_mutexattr_settype 未定义
不过执行时 pthread_mutexattr_settype 工作正常,RECURSIVE的特性能够体现。

我检查了一下<pthread.h>,发现这样一段代码:

#ifdef __USE_UNIX98
/* Return in *KIND the mutex kind attribute in *ATTR. */
extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
__attr, int *__restrict __kind) __THROW;

/* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
PTHREAD_MUTEX_DEFAULT). */
extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
__THROW;

/* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
extern int pthread_mutexattr_getprotocol (__const pthread_mutexattr_t *
__restrict __attr,
int *__restrict __protocol) __THROW;

/* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
int __protocol) __THROW;

/* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
extern int pthread_mutexattr_getprioceiling (__const pthread_mutexattr_t *
__restrict __attr,
int *__restrict __prioceiling)
__THROW;

/* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
int __prioceiling) __THROW;
#endif


我的理解就是只有定义了 __USE_UNIX98 才会有 pthread_mutexattr_settype 的定义

从网络上得知UNIX98的相关知识,我认为自己的SUSE是支持UNIX98的,于是我把程序头改成了这样:

#define __USE_UNIX98
#include <pthread.h>

int main()
{
/* 这部分不变 */
}


可是错误依旧。似乎pthread压根就不理我的 #define

于是我该为:

gcc -D__USE_UNIX98 test.c -lpthread

问题依旧。

求高手赐教。
...全文
1971 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fierygnu 2008-09-05
  • 打赏
  • 举报
回复
有文档描述。看gnu libc的文档,对这些宏有详细描述。
Keyser 2008-09-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fierygnu 的回复:]
-E后的结果没有pthread_mutexattr_settype?
[/Quote]

没有。


我找到解决方法了,加上 -D_GNU_SOURCE
Keyser 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fierygnu 的回复:]
用gcc -E预处理一下看看
[/Quote]

看了一下,总之我的#define __USE_UNIX98 不知道被哪个头文件给 #undef 了
Keyser 2008-09-04
  • 打赏
  • 举报
回复
没有,那段定义被 #ifdef 跳掉了

我最后找到一个解决方案,给编译参数加上 -D_GNU_SOURCE 但是没有文档准确的描述这一点,真他妈操蛋
fierygnu 2008-09-04
  • 打赏
  • 举报
回复
-E后的结果没有pthread_mutexattr_settype?
fierygnu 2008-09-03
  • 打赏
  • 举报
回复
用gcc -E预处理一下看看

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧