【问题标题】:What are __lll_lock_wake_private and __lll_unlock_wake_private, and why are they slowing down my code?什么是 __lll_lock_wait_private 和 __lll_lock_wait_private,为什么它们会减慢我的代码速度?
【发布时间】:2014-10-13 04:16:02
【问题描述】:

我有一个相当简单的 C 程序,它在一个线程上运行比在多个线程上快得多。 (我在四核 i5 处理器上运行。)

通过使用高度科学的"GDB halt debugging" 技术,我确定看起来一次实际上只有一个线程在执行。 基本上,当我在 GDB 中点击 ^C 并输入 info threads 时,我会得到如下信息:

  Id   Target Id         Frame 
  29   Thread 0x7ffff5cec700 (LWP 14787) "corr" __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95
  28   Thread 0x7ffff64ed700 (LWP 14786) "corr" __lll_unlock_wake_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:341
  27   Thread 0x7ffff6cee700 (LWP 14785) "corr" 0x00007ffff752ca2c in __random () at random.c:296
  26   Thread 0x7ffff74ef700 (LWP 14784) "corr" __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95
* 1    Thread 0x7ffff7fd5740 (LWP 14755) "corr" 0x00007ffff78bf66b in pthread_join (threadid=140737342535424, thread_return=0x7fffffffdd80) at pthread_join.c:92

(线程 1 是主线程;线程 26-29 是工作线程。)

快速的 Google 搜索似乎暗示这些功能与死锁检测有关,但除此之外我无能为力。 这些函数是什么,为什么它们会变慢?


可能相关: 如果我在创建每个线程后立即加入每个线程,然后再创建其他线程(即,根本不是真正的多线程,但仍会产生线程开销),则不会发生这种效果,并且我的程序运行得更快。


如果有用,here's 代码转储(159 行)。

【问题讨论】:

    标签: c multithreading optimization parallel-processing


    【解决方案1】:

    您的线程正在争夺随机数生成器。每当一个线程可以访问其上下文时,其他线程必须等到它释放保护它的锁。您应该使用rand_r(或lrand48_r,或任何符合您需求的合理随机数生成器)而不是rand,这样每个线程都有自己的上下文。

    【讨论】:

    • 准确!非常感谢。供参考,this question 提供帮助。
    猜你喜欢
    • 2018-11-30
    • 2012-01-02
    • 2019-01-03
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2018-01-02
    • 2017-12-17
    相关资源
    最近更新 更多