2

If anyone is good with dart (or) other single threaded programming languages, i have this small doubt about the inner workings of the event loop and such and i would like an explanation if possible.

If you're too lazy to goto the link:
1. I have a future returned from a http request.
2. a future.then is declared that prints the http result.
3. A separate while(true) loop is declared that runs forever that just prints natural numbers.
4. the while loop also has an await future.delay that waits for 1ms before continuing with the next iteration

My question :

1. There's only one thread so how does the http download code run WHILE my main loop is still executing.
2. my future.then event is not processed unless i await a future.delay separately for 1ms. returning control to the event loop ? i don't get it how does adding an event help it process a prior event? It's FIFO ?

gist :https://gist.github.com/TheAnimatri...

discussion:
https://groups.google.com/a/...

Comments
  • 1
    My best guess looks like this:
    Just your dart-code runs async.
    The underlying C/CPP-code may run in parallel and pushes values into the VM.
    That way, you don't have to care about multi-threading problems, but it scales nearly like multi-threading.
  • 0
    @metamourge wait so you're saying, the vm interpreter which prolly is written in c/c++ has a tendency to multi thread while interpreting the code in a threaded way.

    Umm... I'm a lil confused. Wouldn't that be multi threaded in general?

    Edit:
    Youre saying we don't have to "care" about it. Gotcha. Multi threading happens automatically in a sense with single threaded code.. Umm this is weirdly difficult to think about XD need a pen and paper. I won't be afraid to tag you again for more discussion xD
  • 0
    There is a well written blog by dartlang people

    https://medium.com/dartlang/...

    Basically, dart uses isolates ( which look like threads but without shared memory ) and an event loop for each isolate to get async working
  • 0
    @AvyChanna I've seen that article a bunch of times, it doesn't seem to explain what happens to non eventful code like the future's internal http code in my question.
  • 1
    The confusion does come from oversimplification.

    It is like what you're describing, to some extent either Javascript or something that Javascript communicates with has to be multithreaded.

    In fact, v8 under the hood runs multiple threads. Most js engines do apparently (and I would believe most mature software does multithread at some level too)

    The user code that people run on a javascript VM does is single threaded though, and by that I mean, a js program (as in, a program that a js VM runs) has no thread manipulation API.

    WebWorkers are available in browsers which is similar to threads, but not nearly as low level.
Add Comment