实际上,手册页中的示例是错误的,在两个地方代码偏离了相应注释中的正确描述。
/* Acquire the futex pointed to by 'futexp': wait for its value to
become 1,and then set the value to 0. */
static void
fwait(int *futexp)
{
int s;
/* atomic_compare_exchange_strong(ptr,oldval,newval)
atomically performs the equivalent of:
if (*ptr == *oldval)
*ptr = newval;
It returns true if the test yielded true and *ptr was updated. */
while (1) {
/* Is the futex available? */
const int zero = 0;
if (atomic_compare_exchange_strong(futexp,&zero,1))
break; /* Yes */
反之亦然:
/* Is the futex available? */
if (atomic_compare_exchange_strong(futexp,&(int){1},0))
break; /* Yes */
/* Release the futex pointed to by 'futexp': if the futex currently
has the value 0,set its value to 1 and the wake any futex waiters,so that if the peer is blocked in fpost(),it can proceed. */
static void
fpost(int *futexp)
{
int s;
/* atomic_compare_exchange_strong() was described in comments above */
const int one = 1;
if (atomic_compare_exchange_strong(futexp,&one,0)) {
…
反之亦然:
if (atomic_compare_exchange_strong(futexp,&(int){0},1)) {
…
本文链接:https://www.f2er.com/2810394.html