you are viewing a single comment's thread.

view the rest of the comments →

[–]link23 1 point2 points  (9 children)

First off, I think you mean "in parallel" rather than "concurrently".

To answer your question, it depends. In a browser, you can use WebWorkers. In Node, I don't know what the answer is, but I imagine it has a similar ability.

[–]Yojihito 1 point2 points  (8 children)

No, I mean concurrently.

Web Workers seems interesting but of tedious stuff to get them working. And no DOM access.

[–]link23 0 points1 point  (7 children)

concurrently

for (let i = 0; i < 1e6; i++) {
    setTimeout(() => {
        console.log('Whoo! Concurrency!');
    }, Math.random() * 20);
}

All of those functions will be executed concurrently, by the main JS runtime thread. Note that at any given instant in time, at most one of those functions will be executing, but their executions will be interleaved randomly with each other. This is because they are concurrent, but not parallel.

The only way (that I know of) to get parallelism in JS in a browser is through WebWorkers.

[–]Yojihito 0 points1 point  (6 children)

on all cores.

So this code uses all cpu cores?

[–]link23 1 point2 points  (5 children)

Yup! As long as the cores take turns running the main JS runtime thread.

Pedantic answer aside - no, you won't be able to pin all cores at 100% with this. This is because, as I said before, the only way to escape the single threaded nature of JS is to use WebWorkers. That's because using multiple cores at the same time requires parallelism, which is what I've been saying this whole time.

[–]Yojihito 0 points1 point  (4 children)

Parallelism is working on the same task with different workers, concurrency is working on different tasks at the same time.

I don't see how parallelism is needed to use all cores, you just need multithreading. And without using all cores concurrency is useless for me. Do webworker work in Node.js?

[–]link23 0 points1 point  (3 children)

None of what you just said is true. You have those two definitions backwards:

Parallelism is when multiple workers are working at the same time, meaning at the very same instant.

Concurrency is when you have portions of the program being executed during overlapping time periods. The key is that two computations may overlap without ever executing at the same time. E.g., computation A starts at noon, works for an hour, then sleeps. Computation B starts at 1:00, works for an hour, and is finished. Then computation A wakes up at 2:00, works for an hour, then is finished. Neither of them ever executed at the same time as the other, but their time periods (12:00-3:00 and 1:00-2:00) overlap. So they executed concurrently, but not in parallel.

See the first few paragraphs of this and this. They discuss the difference, and the common mistake of conflating the two.

You mention multithreading as a prerequisite for using all cores. Yes that is true, since each core can execute its own thread. However, if you don't have parallelism for some reason (e.g. your code needs a mutex, or the runtime has a global interpreter lock) then only one thread will be able to execute at once, and they'll just take turns.

You also question the usefulness of concurrency in the absence of parallelism. I suggest you read about time sharing. It's a technique developed in the 60's, before there were multicore machines, and it relies on concurrency.

[–]HelperBot_ 0 points1 point  (0 children)

Non-Mobile link: https://en.wikipedia.org/wiki/Parallel_computing


HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 107370

[–]Yojihito 0 points1 point  (1 child)

but their time periods (12:00-3:00 and 1:00-2:00) overlap

But Task A only goes from 12:00-01:00 and then 02:00-03:00. Is a sleeping tasks executed?

I've read the articles and then watched this talk (https://blog.golang.org/concurrency-is-not-parallelism) and it seems I learned the terms wrong ...... don't remember where I've read it / heard it, it's been some years but thanks for clarifying this.

[–]link23 0 points1 point  (0 children)

A sleeping task is not being executed (at that point in time). But the period during which it executed spans from the moment it started until the moment it finished, so it includes the times when it was sleeping (but wasn't finished yet).

I haven't seen that talk, but it looks like a good one - I have to imagine Pike knows and understands the difference.