-1

Can't we create a different thread and start a runloop that would listen for touch events or anything related to UI? Is there any research going on to handle UI tasks in multithreaded environment?

Anshu
  • 511
  • 1
  • 5
  • 15
  • 1
    This is very old but may be useful: https://community.oracle.com/blogs/kgh/2004/10/19/multithreaded-toolkits-failed-dream – Anshu Jul 25 '16 at 03:36

1 Answers1

3

UIKit is not internally thread-safe.

I feel like I should explain further, but that really is the entire answer. There is no research in this field outside of Apple, because only Apple maintains UIKit. It is unlikely that they would massively rewrite UIKit to make it thread-safe, not to mention the significant performance penalty they'd impose by doing so. You must do all of your UI event and main context drawing work on the main thread unless explicitly indicated otherwise in the docs.

Perhaps it's worth going a bit further: there is very limited value to a multi-threaded UI. Each pixel can only display one thing at a time. The capacitive touch sensors can only send one signal at a time. There is only one UI. The promise of concurrency and/or parallelism is that I can either better reason about the problem concurrently, or that I can make better use of parallel hardware. I can't actually draw two things at the same time. There is only one screen. Ultimately I am drawing one thing. A bunch of curves, at the end of the day, is still one "picture." It is composited and drawn as a single thing. This is in contrast to computation work. I can actually compute two bezier curves at the same time, fully in parallel, and make more use of the hardware. And that is something that I can do on other threads today.

This isn't to say that there's no parallelism inside of UIKit. There is, both in software and hardware. But there's no major value to taking on the high complexity and performance expense of providing a reentrant API for UI processing. Furthermore, UIs are about the most stateful things you can have, and mutable state is the sworn enemy of multithreaded code. Even if it were valuable, UI code is particularly challenging to make reentrant and thread-safe. This is to more or less true across many platforms, as you note.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • But I feel that in other technologies whether that is new or old, there is no support for multithreading for UI tasks. Is it difficult to achieve or not possible at all? – Anshu Jul 25 '16 at 03:19