- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是想知道 std::this_thread::yield
在 linux 上是如何实现的,它与 sched_yield
有什么不同吗?我看到一些自旋锁实现暗示 std::this_thread::yield
在线程放弃进程的时间方面比 sched_yield
更轻量级,这是真的吗?
最佳答案
执行std::this_thread::yield
在libstdc++ library看起来像这样:
/// yield
inline void
yield() noexcept
{
#ifdef _GLIBCXX_USE_SCHED_YIELD
__gthread_yield();
#endif
}
符号__gthread_yield
由 in gcc in gthr-posix.h 定义从中我们需要以下内容:
# define __gthrw2(name,name2,type) \
static __typeof(type) name \
__attribute__ ((__weakref__(#name2), __copy__ (type))); \
__gthrw_pragma(weak type)
# define __gthrw_(name) __gthrw_ ## name
...
/* Typically, __gthrw_foo is a weak reference to symbol foo. */
#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
...
__gthrw(sched_yield)
...
static inline int
__gthread_yield (void)
{
return __gthrw_(sched_yield) ();
}
所以基本上在 gcc 中调用 std::this_thread::yield
电话 sched_yield
如果_GLIBCXX_USE_SCHED_YIELD
被定义为。你可以找到如果 _GLIBCXX_USE_SCHED_YIELD
在 #include <bits/c++config.h>
中定义,但在 linux x86 或 x86_64 上它很可能已定义。所以std::this_thread::yield
应该调用sched_yield
关于使用 gcc GNU 编译器集合和 libstdc++ GNU C++ 库的实现。
在“libc++”C++ 标准库中 std::this_thread::yield
可以找到函数定义 in libcxx/thread :
inline _LIBCPP_INLINE_VISIBILITY
void yield() _NOEXCEPT {__libcpp_thread_yield();}
符号__libcpp_thread_yield
在 libcxx/threading_support 中定义:
void __libcpp_thread_yield()
{
sched_yield();
}
因此 clang 编译器(即使用 libc++ C++ 标准库的编译器)也调用 sched_yield
在 std::this_thread::yield
.
I am just wondering how std::this_thread::yield is implemented on linux and is it any different from sched_yield?
很可能在大多数 Linux 实现上都是一样的。
I have seen some spinlock implementations that imply std::this_thread::yield being something more lightweight that sched_yield in terms of for how long the threads abandons the process, is it true?
在大多数 Linux 实现上它很可能是错误的。
关于c++ - std::this_thread::yield 与 linux 上的 sched_yield 有什么不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600135/
我对 sched_yield 有几个问题函数,因为我看到它没有按我的代码中的预期运行。很多时候,当我尝试通过调用 sched_yield 来让出它时,即使存在其他线程,我也会看到同一个线程一次又一次地
我们有在循环中使用 sched_yield 的代码。当我们这样做时,我们似乎会降低其他线程的性能,特别是那些涉及内核调用(如 IO 和互斥/事件处理)的线程。我正在尝试确定此行为的确切原因。 过度调用
如果我有一个进程,其所有线程都在不同的内核上运行,那么 sched_yield 在这种情况下会做什么?放弃并立即重新发行?因为我看到的是 sched_yield 不会导致调用它的线程,根本等待任何周期
我正在尝试让 3 个线程打印以元音、辅音和其他开头的单词。这是我的程序: int main(int argc, char** argv) { int i,j,n_thread; pthread_t t
我有 2 个线程:将“重新打印”用户输入语句的元音和辅音。元音线程将打印以元音开头的单词,辅音线程将打印以辅音开头的单词...我正在尝试使用 sched_yield() 使输出与用户输入的顺序相同..
我在 Linux 下有一个进程,由两个线程组成,一个生产者和一个消费者。为简单起见,假设这是在单核、单 CPU 架构上的系统中运行的唯一进程。 生产者首先被创建,并使用 sched_setschedu
SCHED_DEADLINE 的文档第 4.4 点指出: This behavior of sched_yield() allows the task to wake-up exactly at th
我使用的是相当旧的开发环境,当我“man sched_yield”时,我可以看到这个手册页,但是如果我尝试使用它, $猫m.cpp #include int main(){ return 0;
我只是想知道 std::this_thread::yield 在 linux 上是如何实现的,它与 sched_yield 有什么不同吗?我看到一些自旋锁实现暗示 std::this_thread::
我是一名优秀的程序员,十分优秀!