免费注册 查看新帖 |

Chinaunix

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

[内核同步] FUTEX_REQUEUE [复制链接]

论坛徽章:
0
发表于 2015-12-08 11:14 |显示全部楼层
一直没搞懂 FUTEX_REQUEUE的实际用途。看到最多的解释就是为了解决”惊群效应“。但是一直没搞清楚,到底是怎么解决的。
用户态库的实现大体如下:
int pthread_cond_wait(pthread_cond_t* cond_interface, pthread_mutex_t* mutex)
{
     pthread_mutex_unlock(mutex);
     sys_futex(cond_interface, FUTEX_WAIT,1 , NULL, NULL, 0);
     pthread_mutex_lock(mutex);
};


int pthread_cond_broadcast(pthread_cond_t* cond_interface, pthread_mutex_t * mutex) {
     sys_futex(cond_interface, FUTEX_REQUEUE, 1, (void *) INT_MAX, mutex, 0);
}


一直没搞清楚,为什么调用FUTEX_REQUEUE将cond_interface的waiters转移到mutex上去。

论坛徽章:
0
发表于 2015-12-08 11:27 |显示全部楼层
FUTEX_REQUEUE

The Futex API allows more control over the wait-queues than just adding and removing sleeping threads. Sleeping threads can also be moved between two different wait queues. This provides an important optimization for pthreads condition variables.

The typical usage of a condition variable requires using a mutex to lock the access to the condition being tested. If a thread is waiting for the condition to become true, then it sleeps on condition variable's wait queue. However, a "broadcast" event may wake up all such sleepers. They will then need to acquire the mutex in order to test the condition. Since being woken up simply to fall back asleep on the lock's wait-queue is inefficient, it is much better to transfer the majority of the waiters onto the lock's wait-queue. This multiplex function provides this operation.

为什么"a "broadcast" event may wake up all such sleepers" ?

论坛徽章:
0
发表于 2016-03-17 16:31 |显示全部楼层
已解决,请看我的CU博客  futex(2)----requeue使用场景
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP