简体   繁体   English

在唤醒线程时,futex如何比互斥体花费更长的时间?

[英]How come a futex take longer than a mutex on waking a thread?

I was trying to measure the latency when a thread calling to wake up a sleeping thread. 我正在尝试测量线程调用唤醒睡眠线程时的延迟。 Since it has been said that many synchronization premitives are developed on top of futex, I am expecting futex are always faster. 由于已经说过许多同步谓词是在futex之上开发的,所以我希望futex总是更快。 However, my test was giving opposite result. 但是,我的测试却给出相反的结果。 I wonder if I did anything wrong, or it is actually a fact. 我想知道我做错了什么还是实际上是事实。

Here's the detail of my test: 这是我的测试详细信息:

  • Process have been set affinity to a particular core 流程已设置为与特定核心相关
  • time is compared by the number returned from RDTSC() instruction 时间与从RDTSC()指令返回的数字进行比较
  • 2 threads are created, in which the waking thread2 have higher FIFO priority 创建2个线程,其中唤醒线程2具有更高的FIFO优先级

Thread1 is sending signal by unlocking the mutex so the Thread2 is supposed to wake up in next schedule. 线程1通过解锁互斥锁来发送信号,因此线程2应该在下一个计划中唤醒。 The sleep(1) on thread1 is to make sure the thread2 sleep waiting for the mutex at the time mutex_unlock is called. 线程1上的sleep(1)是为了确保线程2在调用mutex_unlock时处于等待互斥的睡眠状态。

void *Thread1(void *dummy)
{
    while(1)
    {
        pthread_mutex_lock( &mutx );
        sleep(1);
        t1 = rdtsc();
        pthread_mutex_unlock( &mutx );
        pthread_yield();
    }
    return NULL;
}

void *Thread2(void *dummy)
{
    while(1)
    {
        pthread_mutex_lock( &mutx );
        t2 = rdtsc();
        if(t1>0)
        {
                    // print out the result
            cout << t2-t1 << endl;
            t1 = 0;
        }
        pthread_mutex_unlock( &mutx );
        pthread_yield();
    }
    return NULL;
}

A similar test is done by replacing the mutex with a futex system call: 通过用futex系统调用替换互斥锁,可以完成类似的测试:

void *Thread1(void *dummy)
{
    while(1)
    {
        sleep(1);
        t1 = rdtsc();
    syscall(SYS_futex, &futx, FUTEX_WAKE, 1);
        pthread_yield();
    }
    return NULL;
}

void *Thread2(void *dummy)
{
    while(1)
    {
        syscall(SYS_futex, &futx, FUTEX_WAIT, 0);
        t2 = rdtsc();
        if(t1>0)
        {
            cout << t2-t1 << endl;
            t1 = 0;
        }
        pthread_yield();
    }
    return NULL;
}

Both the mutx and futx is declared global. mutx和futx都声明为全局。 On my Core i7 930 machine, with fedora17, the mutex consistently wake faster than futex by 5-10%. 在装有fedora17的Core i7 930机器上,互斥锁始终比futex唤醒快5-10%。 The test application was compiled by gcc 4.7 on default setting. 测试应用程序是在默认设置下由gcc 4.7编译的。 Any suggestion? 有什么建议吗? Thanks in advance. 提前致谢。

Futex-based implementation of mutex doesn't do syscall for any lock/unlock operation, but only when needed. 基于互斥体的基于Futex的实现不会对任何锁定/解锁操作执行系统调用,而只会在需要时执行。

When you replace mutex lock/unlock with unconditional futex syscalls, it is necessary slower. 当您用无条件futex syscall替换互斥锁/解锁时,速度会变慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM