在Linux中线程可以暂停的最短时间 [英] Minimum time a thread can pause in Linux

查看:152
本文介绍了在Linux中线程可以暂停的最短时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,线程需要暂停很少的时间(100个时钟周期).暂停的一种方法是调用nanosleep,但我想它需要对内核进行系统调用.现在,我想暂停而不进入内核.

In my application, threads need to pause for a very little time (100s of clock cycles). One way to pause is to call nanosleep, but I suppose it requires a system call to the kernel. Now I want to pause without going to the kernel.

请注意,我有足够的内核来运行我的线程,并且将每个线程绑定到一个单独的内核,因此即使是可以将内核暂停一会儿的指令也将是不错的.我正在使用x86.我只是希望线程在暂停时暂停.我不想繁忙的循环或对内核的系统调用.是否有可能做到这一点?我可以暂停线程的最短时间是什么?

Note that I have enough cores to run my threads on and I bind each thread to a separate core, so even an instruction that can halt the core for a little while would be good. I am using x86. I just want the thread to halt while pausing. I don't want a busy loop or a system call to the kernel. Is it possible to do this? What is the minimum time I can pause a thread?

推荐答案

_mm_pause是解决方法.

不幸的是,它提供的延迟会随每个处理器系列而改变:

Unfortunately the delay it provides can change with each processor family:

http://siyobik.info/main/reference/instruction/PAUSE

Linux上GCC的用法示例:

Example usage for GCC on Linux:

#include <xmmintrin.h>

int main (void) {
    _mm_pause();
    return 0;
}

在启用MMX的情况下进行编译:

Compile with MMX enabled:

gcc -o moo moo.c  -march=native

此外,您始终可以只使用内联汇编程序:

Also you can always just use inline assembler:

__asm volatile ("pause" ::: "memory");

从某些英特尔工程师那里,您可能会发现这对于确定暂停的成本很有用:

From some Intel engineers, you might find this useful to determine the cost of pausing:

NOP指令可以在0.4-0.5个时钟之间,而PAUSE指令则可以 可以消耗38-40个时钟.

NOP instruction can be between 0.4-0.5 clocks and PAUSE instruction can consume 38-40 clocks.

http://software.intel.com/en-us/forums/showthread .php?t = 48371

这篇关于在Linux中线程可以暂停的最短时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆