请教一段代码

wqhhust 2011-04-17 04:32:41
这段代码到底定义了怎么样的一个数据结构啊?
#define pthread_mutex_t my_pthread_fastmutex_t

typedef struct st_my_pthread_fastmutex_t
{
pthread_mutex_t mutex;
uint spins;
uint rng_state;
} my_pthread_fastmutex_t;


我知道以下结构可以定义一个链表。但是my_pthread_fastmutex_t中没有指针,他是个什么东西啊,自己如何包含自己?
struct Node {
int data;
struct Node *nextptr;
};
...全文
125 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqhhust 2011-04-17
  • 打赏
  • 举报
回复
牛人。非常感谢。 还这样用,改变他的定义,容易造成误解
jialejiahi 2011-04-17
  • 打赏
  • 举报
回复


#if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
typedef struct st_my_pthread_fastmutex_t
{
/*这里pthread_mutex_t用的是前面的定义,请找到MY_PTHREAD_FASTMUTEX*/
pthread_mutex_t mutex;
uint spins;
uint rng_state;
} my_pthread_fastmutex_t;
void fastmutex_global_init(void);

int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
const pthread_mutexattr_t *attr);
int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp);

#undef pthread_mutex_init
#undef pthread_mutex_lock
#undef pthread_mutex_unlock
#undef pthread_mutex_destroy
#undef pthread_mutex_wait
#undef pthread_mutex_timedwait
#undef pthread_mutex_t /*注意这一句哦*/
#undef pthread_cond_wait
#undef pthread_cond_timedwait
#undef pthread_mutex_trylock
#define pthread_mutex_init(A,B) my_pthread_fastmutex_init((A),(B))
#define pthread_mutex_lock(A) my_pthread_fastmutex_lock(A)
#define pthread_mutex_unlock(A) pthread_mutex_unlock(&(A)->mutex)
#define pthread_mutex_destroy(A) pthread_mutex_destroy(&(A)->mutex)
#define pthread_cond_wait(A,B) pthread_cond_wait((A),&(B)->mutex)
#define pthread_cond_timedwait(A,B,C) pthread_cond_timedwait((A),&(B)->mutex,(C))
#define pthread_mutex_trylock(A) pthread_mutex_trylock(&(A)->mutex)
/*pthread_mutex_t的这个定义只可能用在下文中,不会用在前面*/
#define pthread_mutex_t my_pthread_fastmutex_t
#endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */

看这种源代码的时候一定要注意前后结合,注意看#if, #ifdef等这些预编译条件,不要断章取义。
jialejiahi 2011-04-17
  • 打赏
  • 举报
回复


#if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
typedef struct st_my_pthread_fastmutex_t
{
/*这里pthread_mutex_t用的是前面的定义,请找到MY_PTHREAD_FASTMUTEX*/
pthread_mutex_t mutex;
uint spins;
uint rng_state;
} my_pthread_fastmutex_t;
void fastmutex_global_init(void);

int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
const pthread_mutexattr_t *attr);
int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp);

#undef pthread_mutex_init
#undef pthread_mutex_lock
#undef pthread_mutex_unlock
#undef pthread_mutex_destroy
#undef pthread_mutex_wait
#undef pthread_mutex_timedwait
#undef pthread_mutex_t /*注意这一句哦*/
#undef pthread_cond_wait
#undef pthread_cond_timedwait
#undef pthread_mutex_trylock
#define pthread_mutex_init(A,B) my_pthread_fastmutex_init((A),(B))
#define pthread_mutex_lock(A) my_pthread_fastmutex_lock(A)
#define pthread_mutex_unlock(A) pthread_mutex_unlock(&(A)->mutex)
#define pthread_mutex_destroy(A) pthread_mutex_destroy(&(A)->mutex)
#define pthread_cond_wait(A,B) pthread_cond_wait((A),&(B)->mutex)
#define pthread_cond_timedwait(A,B,C) pthread_cond_timedwait((A),&(B)->mutex,(C))
#define pthread_mutex_trylock(A) pthread_mutex_trylock(&(A)->mutex)
/*pthread_mutex_t的这个定义只可能用在下文中,不会用在前面*/
#define pthread_mutex_t my_pthread_fastmutex_t
#endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */
wqhhust 2011-04-17
  • 打赏
  • 举报
回复
这个结构是mysql中定义的,可以在下面的link中查到

http://mysql-connector-cplus-pplus-p.sourcearchive.com/documentation/1.1.0~r814-1/my__pthread_8h-source.html

定位到了这一页后,搜索
1:#define pthread_mutex_t my_pthread_fastmutex_t
2:typedef struct st_my_pthread_fastmutex_t

就可以看到这个结构。 我也觉得结构体不能嵌套自身。但Mysql终究是这样定义的,mysql肯定不会有误的:)
jialejiahi 2011-04-17
  • 打赏
  • 举报
回复
如果有#define pthread_mutex_t my_pthread_fastmutex_t
那么就相当于
typedef struct st_my_pthread_fastmutex_t
{
my_pthread_fastmutex_t mutex;
uint spins;
uint rng_state;
} my_pthread_fastmutex_t;

结构体不能嵌套自身,所以这样写应该不对吧。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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