Difference between Thread, Isolate and Process in Dart
Asked Answered
S

3

6

What's the difference between Thread, Isolate and a Process in Dart?

As far as I know Dart is a single-threaded language, but it can spawn many isolates which don't share memories with each other and we can do the heavy lifting work on them and return the result without blocking the UI.

But what Process is for, is that a part of an Isolate? Can anyone describe above three in more detail.

And when we do asynchronous programming using Future and let's see we are doing heavy lifting in it, will that block the UI thread in case it is awaited using the await keyword.

Seely answered 28/8, 2020 at 15:23 Comment(1)
Ahh, found it. More than you ever wanted to know about processes, threads, and isolates: https://mrale.ph/dartvm/Acetabulum
A
1

A Process is a native OS (Unix, Windows, MacOS) construct, which consists of one or more threads with their own address space and execution environment. In Dart, an application consists of one or more threads, one of which is the main UI thread, while the rest are typically called Isolates.

Acetabulum answered 28/8, 2020 at 16:34 Comment(4)
AFAIK a Dart app only has one thread, you can call it "main" or "UI" thread. Main isolate has that single thread which executes the event loop. You can spawn more isolates if need be.Seely
It's my understanding that an Isolate uses green threads. So a dart app can have many threads.Acetabulum
Alright. Dart app can't have many threads like that. It can have many isolates and each of those isolates can only have one thread in them.Seely
"one of which is the main UI thread, while the rest are typically called Isolates." ->All Dart code runs in an isolate. (there can be multiple isolates though)Englis
P
1

In contrast to what the previous answer said, All Dart code runs in an isolate.

Actually, your understanding is correct, based on what you said in the comments.

From the docs (emphasis mine):

Within an app, all Dart code runs in an isolate. Each Dart isolate has a single thread of execution and shares no mutable objects with other isolates. To communicate with each other, isolates use message passing. Although Dart’s isolate model is built with underlying primitives such as processes and threads that the operating system provides, the Dart VM’s use of these primitives is an implementation detail that this page doesn’t discuss.

Many Dart apps use only one isolate (the main isolate), but you can create additional isolates, enabling parallel code execution on multiple processor cores.

Your question hasn't been answered yet, and it will not be answered easily, because it depends on the language implementation.

Also, your another question: "And when we do asynchronous programming using Future and let's see we are doing heavy lifting in it, will that block the UI thread in case it is awaited using the await keyword."

-> Yes, it will block the UI, unless you use another isolate.

Paramecium answered 15/4, 2022 at 3:7 Comment(0)
N
0

Here's what the isolates official docs says.

Instead of threads, all Dart code runs inside isolates. Using isolates, your Dart code can perform multiple independent tasks at once, using additional processor cores if they're available. Isolates are like threads or processes, but each isolate has its own memory and a single thread running an event loop.

There's no doubt. we can treat isolates as threads or processes. The most differences are
each isolate has their own memory space and they communicate each other via message passing.

Nb answered 8/10 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.