免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1882 | 回复: 1

[已解决]APUE线程的一个问题 [复制链接]

论坛徽章:
0
发表于 2008-07-04 09:10 |显示全部楼层
这是一个示例:
#define NHASH 29
#define HASH(fp) (((unsigned long)fp)%NHASH)
struct foo *fh[NHASH];

pthread_mutex_t hashlock = PTHREAD_MUTEX_INITIALIZER;

struct foo {
    int             f_count;
    pthread_mutex_t f_lock;
    struct foo     *f_next; /* protected by hashlock */
    int             f_id;
    /* ... more stuff here ... */
};

struct foo *
foo_alloc(void) /* allocate the object */
{
    struct foo  *fp;
    int         idx;

    if ((fp = malloc(sizeof(struct foo))) != NULL) {
        fp->f_count = 1;
        if (pthread_mutex_init(&fp->f_lock, NULL) != 0) {
            free(fp);
            return(NULL);
        }
        idx = HASH(fp);
        pthread_mutex_lock(&hashlock);
        fp->f_next = fh[idx];/*这行和下面那行合起来什么意思啊,看了好久,没看懂*/
        fh[idx] = fp->f_next;
        pthread_mutex_lock(&fp->f_lock);
        pthread_mutex_unlock(&hashlock);
        /* ... continue initialization ... */
        pthread_mutex_unlock(&fp->f_lock);
    }
    return(fp);
}


[ 本帖最后由 zszjxh 于 2008-7-4 14:06 编辑 ]

论坛徽章:
0
发表于 2008-07-04 13:29 |显示全部楼层
我问过相同的问题
http://bbs.chinaunix.net/viewthread.php?tid=1040146
类似的问题
http://bbs.chinaunix.net/thread-869038-1-1.html

是作者的失误
http://www.apuebook.com/errata.html

Page 374, middle of page. fh[idx] = fp->f_next should be fh[idx] = fp. Actually, I should apologize for this one. I've written this logic (correctly) so many times that I have no good excuse for getting it wrong this time. Same problem occurs again near the top of page 377.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP