Current GUI toolkits (for most mainstream operating systems) are not natively thread safe, in fact, widget redrawing and event processing are most commonly performed from inside a global loop.
So what happens when you have a series of stuff to do in disparate threads and need such threads to update GUI elements? if the GUI does not provide a method then you normally start doing all kinds things, lets see a few of them:
Global Variable Madness: this is the worst of them all, it consists in having a set of globally shared variables (or objects) each one with a lock so from your thread you do something like:
object.lock();
object = somedata;
object.unlock();
Then in your GUI callback method....
object.lock();
someGUILabel.setText(object);
object.unlock();
Ok lets see... this method performs an object update instantly (as soon as it can acquire the object lock of course), however having global vars is not something you might want, but then, what happens when you update widget contents too frequently?
Invoke methods (ala Windows Forms): for each GUI object you have an invoke method, which basically tell the GUI engine to update the widget with the value sent during the invoke, notice that this method performs differed widget updates, so the contents are not instantly updated. This one is good for ultra frequent widget updates.
Separating GUI drawing from event processing: this is a good one, but only meaningful if you are the toolkit author, this is the best way to write a GUI toolkit this days, why? because of the multi-core thing!!! By having both things in different threads it is implied that you're taking in count multi-threaded processing so there is not case here, everything should be done already.There are not too many choices ha? well its up to you choose the best of them and if you know a few more please share them.
No comments:
Post a Comment