[英]C++ Thread: How to pass parameter in main thread to another thread with lambda function
I am trying to pass a bool
flag from the main thread to another thread with lambda
function, but it does not seem to work.我正在尝试使用
lambda
function 将bool
标志从主线程传递到另一个线程,但它似乎不起作用。 My idea is to create a LED_thread
blinks LED every one second while the main thread
does something else.我的想法是在
main thread
执行其他操作时创建一个LED_thread
每隔一秒闪烁一次 LED。 Once the main task
finishes, the stop_blink_flag
will turn on and the LED should stop blinking.一旦
main task
完成, stop_blink_flag
将打开并且 LED 应该停止闪烁。 Here is my work:这是我的工作:
int main() {
bool stop_blink_flag = false;
std::thread LED_thread{ [stop_blink_flag] {
while (!stop_blink_flag)
{
std::cout << "LED is blinking every 1 second\n";
sleep(1);
}
std::cout << "\t\tLED STOP BLINKS\n";
} };
// while the main function is running
std::cout << "\t\Main thread starts...\n";
std::cout << "I'm gonna do 1\n";
std::cout << "And I'm gonna do 2\n";
std::cout << "And I'm gonna sleep for 10s\n";
sleep(10);
std::cout << "I just wake up after 10 seconds\n";
std::cout << "I'm gonna go back to sleep for 3 seconds\n";
sleep(3);
std::cout << "I just wake up after 3 seconds\n";
std::cout << "\t\tSystem stops. Now the LED should stop blinking\n";
stop_blink_flag = true;
LED_thread.join(); // main waits for t to finish
}
The result shows that the stop_blink_flag
was not updated from main thread
to LED_thread
, so the LED keeps blinking.结果表明
stop_blink_flag
没有从main thread
更新到LED_thread
,所以 LED 一直闪烁。 The detail result is as followed:详细结果如下:
LED is blinking every 1 second
Main thread starts...
I'm gonna do 1
And I'm gonna do 2
And I'm gonna sleep for 10s
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
I just wake up after 10 seconds
I'm gonna go back to sleep for 3 seconds
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
I just wake up after 3 seconds
System stops. Now the LED should stop blinking
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
LED is blinking every 1 second
Any help or advice would be very helpful and appreciated!任何帮助或建议都会非常有帮助和感激!
Right now you are passing the variable by value - the lambda makes a copy.现在您正在按值传递变量 - lambda 进行复制。
To pass it by reference instead - so the lambda accesses the original variable - just put &
before it:改为通过引用传递它 - 所以 lambda 访问原始变量 - 只需将
&
放在它之前:
// before
std::thread LED_thread{ [stop_blink_flag] {
// after v
std::thread LED_thread{ [&stop_blink_flag] {
Note that since the lambda now accesses the original variable, it's not safe for the lambda to use it after main
returns (which destroys the variable).请注意,由于 lambda 现在访问原始变量,因此 lambda 在
main
返回后使用它是不安全的(这会破坏变量)。 In your case, this is safe because LED_thread.join()
waits for the lambda to return.在您的情况下,这是安全的,因为
LED_thread.join()
等待 lambda 返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.