简体   繁体   English

什么是纺线?

[英]What is a Spinning Thread?

I have stumbled upon the term spinning, referring to a thread while reading this (ROS)我偶然发现了旋转这个词,在阅读 本文时指的是一个线程(ROS)

What is the general concept behind spinning a thread?旋转线程背后的一般概念是什么?

My intuition would say that a spinning thread is a thread that keeps executing in a multithreading process with a certain frequency, somewhat related to the concept of polling (ie keep checking some condition with a certain frequency) but I am not sure at all about it.我的直觉会说,旋转线程是一个在多线程进程中以一定频率持续执行的线程,这与轮询的概念有些相关(即以一定频率不断检查某些条件),但我对此完全不确定.

Could you give some explanation?你能解释一下吗? The more general the better.越通用越好。

There are a couple of separate concepts here.这里有几个单独的概念。

In terms of ROS (the link you reference), ros::spin() runs the ROS callback invoker, so that pending events are delivered to your program callbacks via a thread belonging to your program.就 ROS(您引用的链接)而言, ros::spin()运行 ROS 回调调用程序,以便通过属于您的程序的线程将挂起的事件传递给您的程序回调。 This sort of call typically does not return;这种调用通常不会返回; it will wait for new events to be ready, and invoke appropriate callbacks when they occur.它将等待新事件准备好,并在它们发生时调用适当的回调。

But you also refer to "spinning a thread."但是您也指“旋转线程”。

This is a separate topic.这是一个单独的主题。 It generally relates to a low level programming pattern whereby a thread will repeatedly check for some condition being met without being suspended.它通常与低级编程模式有关,其中线程将重复检查是否满足某些条件而不会被挂起。

A common way to wait for some condition to be met is to just wait on a conditional variable.等待满足某些条件的一种常见方法是仅等待条件变量。 In this example, the thread will be suspended by the kernel until some other thread calls notify on the condition variable.在这个例子中,线程将被 kernel 挂起,直到其他线程调用通知条件变量。 Upon the notify, the kernel will resume the thread, and the condition will evaluate to true, allowing the thread to continue.收到通知后,kernel 将恢复线程,条件将评估为真,允许线程继续。

std::mutex m;
std::condition_variable cv;
bool ready = false;
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{ return ready; });  /* thread suspended */

Alternatively a spinning approach would repeatedly check some condition, without going to sleep.或者,旋转方法会反复检查某些情况,而不会进入睡眠状态。 Caution: this results in high CPU, and there are subtle caveats to implementing correctly).注意:这会导致高 CPU,并且正确实施有一些细微的警告)。

Here is an example of a simple spinlock (although note that spinning threads can be used for other purposes than spinlocks).是一个简单的自旋锁的示例(尽管请注意,自旋线程可以用于自旋锁以外的其他目的)。 In the below code, notice that the while loop repeatedly calls test_and_set ... which is just an attempt to set the flag to true;在下面的代码中,请注意 while 循环重复调用test_and_set ... 这只是尝试将标志设置为 true; that's the spin part.那是旋转部分。

// spin until true
std::atomic_flag lock = ATOMIC_FLAG_INIT;

while (lock.test_and_set(std::memory_order_acquire));  // acquire lock
/* got the flag .. do work */
lock.clear(std::memory_order_release);               // release lock

spin like while loop without sleeping, your task consumes cpu resource constantly until the conditions is satisfied.在没有睡眠的情况下像 while 循环一样旋转,您的任务会不断消耗 cpu 资源,直到满足条件。

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

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