PolymorphismPrince
PolymorphismPrince

Reputation: 307

How does parallel.js do multithreaded programming in Javascript?

I thought that javascript was limited to a single thread in every browser. Parallel.js says:

Parallel.js solves that problem by giving you high level access to multi-core processing using web workers.

The unminified code can be found here. I'm simply wondering how parallel.js works, can anyone explain?

Upvotes: 0

Views: 180

Answers (1)

alexrqs
alexrqs

Reputation: 193

So while javascript has a single thread to run the main set of functions of the stack it also has Workers https://developer.mozilla.org/en-US/docs/Web/API/Worker, these objects are allowed by the browser to run background tasks like complex math operations.

therefore this tasks will be administrated by the browser taking advantage of the OS to create multiple threads for different tasks.

but this does not allow to reuse the same variables in both contexts unless they are used with the recommended API (as a limitation).

and that's what parallel js takes advantage of. check the code and search for worker I'm sure it will illustrate you more.

In summary Workers are an API to run multiple processes that interact by methods instead of the usual js chain of code and your code has to be very specific about what to run and with what variables and kind works like promises where you send a task and you wait for the task to return a result sometime in the future .then() you do something else

Upvotes: 3

Related Questions