将变量从一个线程输出到另一个线程

发布于 2025-01-15 17:14:35 字数 63 浏览 5 评论 0原文

我有一个线程正在无限循环运行,不断更新变量。如何在另一个线程/线程外部访问该变量的实时值(而不处于无限循环中)?

I have a thread that has an infinite loop running, updating a variable constantly. How can I access the live value of that variable in an another thread/outside of the thread (without being in an infinite loop)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2025-01-22 17:14:35

最好的方法是使用全局变量。
但是,当线程同时读取和写入一个变量时,您需要了解锁定条件。

例如,有一个很好的手册。

the best way to do it is to use global variables.
But, you need to be aware of the lock conditions when threads are reading and writing to one variable at once.

There is a good manual for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文