Hacker News new | past | comments | ask | show | jobs | submit login

You want a multi-threaded GUI when it's running a long task. Otherwise it won't update until it's finished running, there's no way to cancel the task, etc.



Uh-huh.

Typically GUI work is instantiated with the GUI toolkit on the call stack. It calls foo.onClick(), etc. Now, if one particular onClick starts long-running task, then there are three possible designs:

Either that particular onClick() starts a worker thread and returns before the worker is done.

Or the GUI toolkit delivers the onClick() in a thread of its own, e.g. from a pool of workers.

Or everything is done in one thread, and the UI blocks.

The last one seems sucky, but the insidiously sucky one is the one in the middle. That's where every user's implementation of onFocusOut() must take care to lock because all of bar.onFocusOut(), foo.onFocusIn(), foo.onMouseUp() and foo.onClick() are called concurrently in four different worker threads. The tail wags the dog.


You don't need a multi-threaded GUI for that. You can have the GUI running on one thread while the task runs on another.


Sure. You put a message queue in there and create command objects for all the updates that tasks might want to make to the UI. It's not hard; in fact it's thoroughly mechanical, so it's exceedingly tedious to program.

So why isn't the computer doing it for me? I'd happily sacrifice some performance if I could just write the change I wanted to make in the thread where I wanted to make it, and have the computer take care of the bookkeeping.


You can just send a closure to the UI thread and have it executed there, provided you are using a good enough programming language.

Or start the long-running computation from the UI code in a way that returns a promise, and chain the UI update on it, in a way that causes that continuation to be scheduled on the UI thread.

Or have an UI that can be updated from any thread (but not simultaneously) and take the big UI lock.


In modern frameworks like Qt the message loop is mostly hidden.

An astute observer will also note that at the bottom of every Win32 program is a message loop monitoring events such as for when the window is closed.


> it's exceedingly tedious to program.

C++ programmer detected? ;)


Just have the ability to send closures (std::function) to other threads' message queues. Then you can use inline lambda syntax.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: