The main application runs in a single thread and then I/O activities happen in an event loop running with a small thread pool.
Once one of those activities completes the event loop will give a signal to the main thread of the Node app and the callback will be executed.
Not sure if that helped or just confused everyone.
Re:An Absolute Beginner's Guide to Node.js
by oh_teh_meows on Tuesday August 20, 2013 @07:28PM
(#6247276)
So basically if you have an infinite loop in your app's one and only thread, none of the callbacks will happen because they have to run on that one thread?
Re:An Absolute Beginner's Guide to Node.js
by thedufer on Wednesday August 21, 2013 @12:41AM
(#6248225)
Yep. Similar to how an infinite loop in the browser will freeze up the page - the browser only gets to control to draw by putting a draw function in the event loop, so if you hog the thread, the page can't update.
Re:An Absolute Beginner's Guide to Node.js
by catnaroek on Tuesday August 20, 2013 @08:20PM
(#6247461)
Remember the "Node.js is Cancer" troll? It was pretty much about this.
Basically, yes.
Re:An Absolute Beginner's Guide to Node.js
by svachalek on Tuesday August 20, 2013 @05:29PM
(#6246706)
No, Javascript has no notion of threads. Rather than true concurrency and a synchronization mechanism, it has callbacks (upon callbacks upon callbacks).
Re:An Absolute Beginner's Guide to Node.js
by jbmartin on Tuesday August 20, 2013 @07:02PM
(#6247156)
Technically speaking, HTML 5 introduced web-workers which allows javascript to have multiple threads (see 1). However, it's still useful to think about them as being single threaded since workers are sandboxed, meaning they don't have access to the global namespace. This avoids issues commonly found in concurrent programming. E.g., separate threads cannot read and write to the same variable since they don't have access to each other's namespaces.
[1] https://developer.mozilla.org/en-US/docs/Web/Guide/Performan...
Re:An Absolute Beginner's Guide to Node.js
by thedufer on Wednesday August 21, 2013 @12:38AM
(#6248214)
While that's true, I don't believe it's applicable to node.js, so it's a bit out of scope for this article.
Re:An Absolute Beginner's Guide to Node.js
by jbmartin on Wednesday August 21, 2013 @03:31AM
(#6248717)
People coming from multi-threaded server environments should be aware that multi-threading does in fact exist in Node (scope of parent comment), but it's rather different from other more common concurrent event handling schemes. I.e., you cannot have shared mutable variables across threads (scope of article and ongoing async discussion). For examples, see https://npmjs.org/package/webworker-threads and https://github.com/cramforce/node-worker.
Re:An Absolute Beginner's Guide to Node.js
by thedufer on Wednesday August 21, 2013 @09:28AM
(#6250080)
Fascinating. I didn't know that webworkers were available in node.js. I stand corrected, then.
Re:An Absolute Beginner's Guide to Node.js
by YcombRegBroken on Tuesday August 20, 2013 @06:13PM
(#6246947)
Callbacks all the way down. I blame 42% of my recent gray hair on callbacks.
Do you mean multi-threaded?