Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

multithreading - What does it mean to "lock" an app's UI thread?

I'm using the flkt-rs crate to design a GUI. I was wondering what it means to "lock" the UI thread of my application. Does this mean that no CPU is being allocated to that thread? Why would you want to lock the UI thread? When would then want to unlock it?

Additionally I see that that the fltk::app::lock() function returns a result, which implies that there might be some issues associated with using it. What could prevent you from succesfully locking the UI thread?

lock function from the fltk-rs docs:

pub fn lock() -> Result<(), FltkError>
question from:https://stackoverflow.com/questions/65944979/what-does-it-mean-to-lock-an-apps-ui-thread

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the FLTK C++ documentation:

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.

So you wouldn't lock the UI for long periods of time; just enough time to safely update the state of a widget, and then unlock it again.

What could prevent you from succesfully locking the UI thread?

This may be addressed by the next paragraph in the same documentation:

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.

If the application is compiled to be single-threaded then locking the main thread would lock the entire application permanently. It doesn't explicitly say it, but it seems reasonable that there would be a FailedToLock error in this situation.

This kind of API doesn't feel very "Rusty" and it's likely like this because the fltk-rs crate is a thin wrapper around the C++ library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...