"lock" 应用程序的 UI 线程意味着什么?
What does it mean to "lock" an app's UI thread?
我正在使用flkt-rs crate设计一个GUI。我想知道“锁定”我的应用程序的 UI 线程是什么意思。这是否意味着没有 CPU 被分配给该线程?为什么要锁定 UI 线程?那什么时候想解锁呢?
此外,我看到 fltk::app::lock()
函数 returns 结果,这意味着使用它可能会出现一些问题。什么会阻止您成功锁定 UI 线程?
来自 fltk-rs docs 的锁定函数:
pub fn lock() -> Result<(), FltkError>
In a multithreaded program, drawing of widgets (in the main()
thread) happens asynchronously to widgets being updated by worker threads, so no drawing can occur safely whilst a widget is being modified (and no widget should be modified whilst drawing is in progress).
FLTK supports multithreaded applications using a locking mechanism internally. This allows a worker thread to lock the rendering context, preventing any drawing from taking place, whilst it changes the value of its widget.
所以您不会长时间锁定 UI;足够的时间来安全地更新小部件的状态,然后再次解锁它。
What could prevent you from succesfully locking the UI thread?
这可能会在同一文档的下一段中解决:
To incorporate the locking mechanism in the library, FLTK must be compiled with –enable-threads
set during the configure process. IDE-based versions of FLTK are automatically compiled with the locking mechanism incorporated if possible. Since version 1.3, the configure script that builds the FLTK library also sets –enable-threads by default.
如果应用程序被编译为单线程,那么锁定主线程将永久锁定整个应用程序。它没有明确说明,但在这种情况下会出现 FailedToLock
错误似乎是合理的。
这种 API 感觉不是很“生锈”,很可能是这样,因为 fltk-rs
crate 是 C++ 库的薄包装。
该函数已公开,因为在处理 xthreads(x11 线程)时可能需要它。否则你不应该需要它。一般来说Fl::lock是递归锁,可以多次调用不会死锁。在内部所有小部件在修改之前调用锁然后 Fl::unlock.
我正在使用flkt-rs crate设计一个GUI。我想知道“锁定”我的应用程序的 UI 线程是什么意思。这是否意味着没有 CPU 被分配给该线程?为什么要锁定 UI 线程?那什么时候想解锁呢?
此外,我看到 fltk::app::lock()
函数 returns 结果,这意味着使用它可能会出现一些问题。什么会阻止您成功锁定 UI 线程?
来自 fltk-rs docs 的锁定函数:
pub fn lock() -> Result<(), FltkError>
In a multithreaded program, drawing of widgets (in the
main()
thread) happens asynchronously to widgets being updated by worker threads, so no drawing can occur safely whilst a widget is being modified (and no widget should be modified whilst drawing is in progress).FLTK supports multithreaded applications using a locking mechanism internally. This allows a worker thread to lock the rendering context, preventing any drawing from taking place, whilst it changes the value of its widget.
所以您不会长时间锁定 UI;足够的时间来安全地更新小部件的状态,然后再次解锁它。
What could prevent you from succesfully locking the UI thread?
这可能会在同一文档的下一段中解决:
To incorporate the locking mechanism in the library, FLTK must be compiled with
–enable-threads
set during the configure process. IDE-based versions of FLTK are automatically compiled with the locking mechanism incorporated if possible. Since version 1.3, the configure script that builds the FLTK library also sets –enable-threads by default.
如果应用程序被编译为单线程,那么锁定主线程将永久锁定整个应用程序。它没有明确说明,但在这种情况下会出现 FailedToLock
错误似乎是合理的。
这种 API 感觉不是很“生锈”,很可能是这样,因为 fltk-rs
crate 是 C++ 库的薄包装。
该函数已公开,因为在处理 xthreads(x11 线程)时可能需要它。否则你不应该需要它。一般来说Fl::lock是递归锁,可以多次调用不会死锁。在内部所有小部件在修改之前调用锁然后 Fl::unlock.