关于互斥量《APUE》
时间:2010-07-05
来源:互联网
本帖最后由 blackgenius 于 2010-07-05 18:08 编辑
CU的高手们,请指教一下APUE里面的一段关于互斥量的程序,本人菜鸟,无法理解,请求指点:
其中有两句,真不知道什么意思,
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
请指点迷津
复制代码
CU的高手们,请指教一下APUE里面的一段关于互斥量的程序,本人菜鸟,无法理解,请求指点:
其中有两句,真不知道什么意思,
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
请指点迷津
- #include <stdlib.h>
- #include <pthread.h>
-
- #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);
- }
-
- void
- foo_hold(struct foo *fp) /* add a reference to the object */
- {
- pthread_mutex_lock(&fp->f_lock);
- fp->f_count++;
- pthread_mutex_unlock(&fp->f_lock);
- }
-
- struct foo *
- foo_find(int id) /* find an existing object */
- {
- struct foo *fp;
- int idx;
-
- idx = HASH(fp);
- pthread_mutex_lock(&hashlock);
- for (fp = fh[idx]; fp != NULL; fp = fp->f_next) {
- if (fp->f_id == id) {
- foo_hold(fp);
- break;
- }
- }
- pthread_mutex_unlock(&hashlock);
- return(fp);
- }
-
- void
- foo_rele(struct foo *fp) /* release a reference to the object */
- {
- struct foo *tfp;
- int idx;
-
- pthread_mutex_lock(&fp->f_lock);
- if (fp->f_count == 1) { /* last reference */
- pthread_mutex_unlock(&fp->f_lock);
- pthread_mutex_lock(&hashlock);
- pthread_mutex_lock(&fp->f_lock);
- /* need to recheck the condition */
- if (fp->f_count != 1) {
- fp->f_count--;
- pthread_mutex_unlock(&fp->f_lock);
- pthread_mutex_unlock(&hashlock);
- return;
- }
- /* remove from list */
- idx = HASH(fp);
- tfp = fh[idx];
- if (tfp == fp) {
- fh[idx] = fp->f_next;
- } else {
- while (tfp->f_next != fp)
- tfp = tfp->f_next;
- tfp->f_next = fp->f_next;
- }
- pthread_mutex_unlock(&hashlock);
- pthread_mutex_unlock(&fp->f_lock);
- pthread_mutex_destroy(&fp->f_lock);
- free(fp);
- } else {
- fp->f_count--;
- pthread_mutex_unlock(&fp->f_lock);
- }
- }
- 105,1 底端
作者: blackgenius 发布时间: 2010-07-05
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
这个写错了,应为:
fp->f_next = fh[idx];
fh[idx] = fp;
另外,find那个算法也错了
fh[idx] = fp->f_next;
这个写错了,应为:
fp->f_next = fh[idx];
fh[idx] = fp;
另外,find那个算法也错了
作者: liwangli1983 发布时间: 2010-07-05
回复 liwangli1983
谢谢指点啊~我菜鸟一个,自己是没法发现这些错误的~
谢谢指点啊~我菜鸟一个,自己是没法发现这些错误的~
作者: blackgenius 发布时间: 2010-07-05
回复 liwangli1983
能帮忙解释下
idx = HASH(fp);
pthread_mutex_lock(&hashlock);
fp->f_next = fh[idx];
fh[idx] = fp;
pthread_mutex_lock(&fp->f_lock);
pthread_mutex_unlock(&hashlock);
/* ... continue initialization ... */
pthread_mutex_unlock(&fp->f_lock);
这段代码是什么意思?谢谢啦啊
能帮忙解释下
idx = HASH(fp);
pthread_mutex_lock(&hashlock);
fp->f_next = fh[idx];
fh[idx] = fp;
pthread_mutex_lock(&fp->f_lock);
pthread_mutex_unlock(&hashlock);
/* ... continue initialization ... */
pthread_mutex_unlock(&fp->f_lock);
这段代码是什么意思?谢谢啦啊
作者: 单眼皮大姐 发布时间: 2010-07-05
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28