Why Dart and Flutter is a single-threaded framework (and it is a great thing)
Right out-of-the-box, Flutter is a single-threaded framework. However, it is interesting to know why it is like this and why so many modern languages uses a scheme that was common in old, but gold, C/C++. Let’s dive into it. Dart (a programming language used by Flutter) is a single-threaded language that thread starts from the […]
Technologies
Right out-of-the-box, Flutter is a single-threaded framework. However, it is interesting to know why it is like this and why so many modern languages uses a scheme that was common in old, but gold, C/C++.
Let’s dive into it.
Dart (a programming language used by Flutter) is a single-threaded language that thread starts from the MAIN function. At first glance, it looks like C++/C and confuses developers who are accustomed to a modern multi-threading language. So how can Dart compete with native mobile languages such as Java, Kotlin, and Swift that fully support multi-threading?
The answer is Isolates. The concept of Isolates is relatively new to concurrency and asynchronous programming. Isolates combine a thread with a special feature called “Event-Loops”. The event loop functions akin to an infinite queue or loop.
All Dart code runs in isolates.
While these are similar to threads, they differ in that each isolate has its isolated memory. As they don’t share state in any way, they can only communicate by messaging. By default, Flutter applications execute all tasks within a solitary isolate — the main isolate.
Dart uses this model, because it generally allows for simpler programming. In most cases, it’s also fast enough to eliminate the risk of an application’s UI becoming unresponsive. Also if developers need to use real thread they can use it with C/C++ inserts, but it is a real edge case.
All dart code in the Flutter app runs in the Isolate. It’s up to the developer whether it’s a main Isolate or a worker isolate. The main isolate is created for the app and developers don’t have to do anything else here. The main function starts on the Main Isolate. Once developers have the main function running, they can start spawning new isolates.
Conclusion
Even though Flutter is technically single-threaded, it is still capable of handling asynchronous operations. Developers can use isolates to perform multiple independent tasks simultaneously, leveraging additional cores if they are available.
Isolates are fast, modern, and easy to use, making them a much simpler alternative to real threads. Additionally, if developers need to use real multithreading, Dart allows them to implement it through C/C++. However, such a need rarely arises in practice.
To find out more about how Swan Software Solutions can help your company with its technology needs, schedule a free assessment.