C futex手册页演示结果不正确
提供了一个简单的演示,但我无法得到页面描述的结果,结果似乎是在我的机器linux 5.2.1上死锁;父进程不会被其子进程唤醒。手册页错了吗 我的机器上的输出示例:C futex手册页演示结果不正确,c,linux,mutex,futex,C,Linux,Mutex,Futex,提供了一个简单的演示,但我无法得到页面描述的结果,结果似乎是在我的机器linux 5.2.1上死锁;父进程不会被其子进程唤醒。手册页错了吗 我的机器上的输出示例: [root@archlinux ~]# ./a.out Child (12877) 0 Parent (12876) 0 Child (12877) 1 // block here 我的系统: [root@archlinux ~]# uname -a Linux archlinux 5.2.1-arch1-1-ARCH #1 S
[root@archlinux ~]# ./a.out
Child (12877) 0
Parent (12876) 0
Child (12877) 1
// block here
我的系统:
[root@archlinux ~]# uname -a
Linux archlinux 5.2.1-arch1-1-ARCH #1 SMP PREEMPT Sun Jul 14 14:52:52 UTC 2019 x86_64 GNU/Linux
实际上,手册页中的示例是错误的,代码在两个地方偏离了相应注释中的正确描述
/* 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 */
if (atomic_compare_exchange_strong(futexp, &(int){0}, 1)) {
…
必须反过来:
/* Is the futex available? */
if (atomic_compare_exchange_strong(futexp, &(int){1}, 0))
break; /* Yes */
if (atomic_compare_exchange_strong(futexp, &(int){0}, 1)) {
…
你能发布完整的uname-a输出吗?无法在我的Ubuntu系统上复制请检查手册页的版本,因为这些都是特定于每个内核版本的。仅供参考。现已修复: