mournjust 发表于 2015-12-08 11:14

FUTEX_REQUEUE

一直没搞懂 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上去。

mournjust 发表于 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" ?

mournjust 发表于 2016-03-17 16:31

已解决,请看我的CU博客futex(2)----requeue使用场景
页: [1]
查看完整版本: FUTEX_REQUEUE