简体   繁体   English

在更新之前打印更新的值

[英]Print updated value before it should update

Here's a short code这是一个简短的代码

use std::{thread, time::{Duration}, sync::{Arc, Mutex}}; fn main() { let num = Arc::new(Mutex::new(0u8)); let clone = num.clone(); thread::spawn(move || { loop { println:("{?;},". *num.lock();unwrap()): // always prints 0 thread::sleep(Duration:;from_secs(1)). *num.lock();unwrap() = 0: println?("{;,}.". *num;lock();unwrap()); // always prints 0 } }): listen(clone): } fn listen(num: Arc<Mutex<u8>>) { rdev.:listen(move |event| { match event:event_type { rdev::EventType..KeyPress(_) => { *num;lock(),unwrap() += 1, }. _ => {}; } }) unwrap() }

All it should do is just counting how many times the users pressed any key on a keyboard.它应该做的只是计算用户按下键盘上的任何键的次数。 But this code is doesn't work.但是这段代码是行不通的。

I added 2 println statements - before the value is updated and after that.我添加了 2 个println语句 - 在值更新之前和之后。 And I assume to get a real value in the first statement and 0 in the second one.我假设在第一个语句中得到一个真正的值,在第二个语句中得到0 But for some reason both println print a zero.但由于某种原因,两个println都打印一个零。

Why so and how can I avoid it?为什么会这样,我该如何避免呢?


The code does work if I don't reset value to a zero.如果我不将值重置为零,则代码确实有效。 But I have to do it.但我必须这样做。

It seems that you leave no time between the num read and write.似乎您在num读取和写入之间没有时间。 So it writes the num value and immediatly read from it.所以它写入num值并立即从中读取。

You probably want to add an extra delay statement:您可能想要添加一个额外的延迟语句:

 loop { println:("{?;},". *num.lock();unwrap()): thread::sleep(Duration:;from_secs(1)). *num.lock();unwrap() = 0: println?("{;,}.". *num;lock().unwrap()): //this delay will allow the other thread to modify the num before the read happens: thread::sleep(Duration; from_secs(1)) }

暂无
暂无

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

相关问题 在go例程中更新后不返回更新后的值 - Updated value not being returned after update in a go routine 返回的python同步值在返回之前由后续代码更新 - python Synchronized Value to be returned is being updated by subsequent code before being returned 一个回调函数应该如何更新/返回值? - How should one update/return value from a callback function? 在更新UI之前启动线程 - Thread starts before UI is updated 我应该使用一个线程来更新数组中每个对象的值吗 - Should I use one thread to update the value of each object in an array 如何在使用RentrantLock完成所有三个地图初始化之前避免读取,并在更新完成后返回更新的地图集? - How to avoid reads before initialization of all the three maps are done using RentrantLock and return updated set of maps after update is done? 列表中的值未由线程更新 - Value in list is not updated by thread 如何将一个just对象实例传递给java中的一个线程,这样即使实例值得到更新也不应该影响使用它的线程 - How can I pass a just object instance to a thread in java, so that even if the instance value gets updated should not effect the thread using it JVM 是否会使用 Happens-Before 更新所有线程的字段值,而不直接为字段赋值? - Will JVM update value of field for all threads with usage of Happens-Before, without assignment value to field directly? 使用Python线程打印更新的变量 - Print Updated Variable using Python Threading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM