13

The Qt doc says,

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread.

The Android doc says,

Like activities and the other components, services run in the main thread of the application process

And iOS,

It is strongly recommended not to update UI controls etc from a background thread (e.g. a timer, comms etc). This can be the cause of crashes which are sometimes very hard to identify. Instead use these to force code to be executed on the UI thread (which is always the “main” thread).

Why does they use a single threaded model to update UI ?

Carina
  • 2,260
  • 2
  • 20
  • 45
  • It increases complexity and has no benefit. See also http://stackoverflow.com/questions/5544447/why-are-most-ui-frameworks-single-threaded – Frank Osterfeld Aug 02 '12 at 08:36

2 Answers2

26

The short answer is, it's the only reasonable way to ensure that the display is not corrupted.

The long answer is that allowing multiple threads to update the UI results in deadlocks, race conditions, and all sorts of trouble. This was the painful lesson taught by Java's AWT (among other UI systems) that allows multiple threads to touch the UI. See, for instance, Multithreaded toolkits: A failed dream?. That post refers (via dead links) to Why Threads Are A Bad Idea and Threadaches.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Multithreaded toolkits link leads to 404 now. Is there somewhere a valid link? – mixtly87 Nov 12 '20 at 15:48
  • @mixtly87 - Hm. It seems to have disappeared from the web. It seems like it's even been removed from the [Internet Archive](https://archive.org/details/DatabasesAndInformationSystems?q=multithread+toolkits+%22a+failed+dream%22). Bummer. – Ted Hopp Nov 12 '20 at 18:22
  • 1
    @mixtly87 - I found a copy here: https://titanwolf.org/Network/Articles/Article?AID=f11696d7-ca6f-44f4-82ef-e4cdc0b264e5#gsc.tab=0 – Ted Hopp Nov 29 '20 at 03:19
0

iOS and Android force you to work with UI only from main thread. The reason is the same as a shared object, thread safe[About]... in multithread environment

Android example error

FATAL EXCEPTION: Thread-19449
E/AndroidRuntime: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

iOS example error

This application is modifying the autolayout engine from a background thread" error?
yoAlex5
  • 29,217
  • 8
  • 193
  • 205