News for hackers. Stuff that matters.
Re:“Node.js is one of the worst things to happen to the soft... by tigershark on Tuesday August 23, 2016 @01:40AM (#12341646)
Mainstream processors are synchronous. For this reason there is a global clock needed to synchronise all (most) the chip parts. I remember there was some research on asynchronous CPUs but I don't think it ever yielded anything commercial.

EDIT: Wikipedia link: https://en.m.wikipedia.org/wiki/Asynchronous_circuit

What you are saying is that they react to interrupts asynchronously, but this has nothing to do with the synchronous nature of a CPU. Moreover I think that if you have a CPU that doesn't respect the barriers and reorders the instructions to execute them in parallel, then it's a bugged CPU.

Re:“Node.js is one of the worst things to happen to the soft... by the_duke on Tuesday August 23, 2016 @02:22AM (#12341782)
That's besides the point.

The things that are synchronous within the processor are also synchronous in Javscript.

What is async in JS and Node (unless you use the sync alternatives) is any kind of IO (disk, network). That is also async for the CPU.

And even the things that you would expect to be synchronous inside the CPU are really NOT for modern, out of order CPUs. They do a lot of complicated things to make code run faster, use branch prediction to predict the code path taken and execute (apparently synchronous) instructions in parallel.

Re:“Node.js is one of the worst things to happen to the soft... by notalaser on Tuesday August 23, 2016 @03:56AM (#12342078)
But that only happens when the result is the same as if the instructions were ran synchronously, and synchronization primitives are still needed at the end of these sections.

Even if they weren't, the abstraction layer that you work at with Node is far, far above the point where you care about instruction reordering. If synchronicity is an inherent property of the process that you're implementing (e.g. you want to insert something in the database and then log an event) then no amount of shuffling will alleviate the fact that the logging has to happen after the database insertion.

Edit: the fact that deep down they reorder instructions is a bit scholastic, but if we are to go into these details, it's worth remembering that even the instruction reordering happens synchronously, and that even asynchronous events, like input from an off-chip peripheral, are treated synchronously. The turtles are synchronous all the way :-).

Re:“Node.js is one of the worst things to happen to the soft... by the_duke on Tuesday August 23, 2016 @06:48AM (#12342675)
Haha yeah, sure. Reading the input from an IO device once it's been delivered is synchronous.

But IO is not handled in an synchronous fashion. If CPU cores just waited around idling until a hard drive is done fetching the data (for example), our computers would be very slow indeed.

That was my main point.

Re:“Node.js is one of the worst things to happen to the soft... by notalaser on Tuesday August 23, 2016 @10:40AM (#12344069)
Ah, certainly. In a similar manner, what I meant is that oftentimes it's the process itself that's synchronous, and if the semantics of a language or a toolkit make it difficult to manipulate this sort of flow, people complain.

I've done too little work with Node to have a constructive opinion on it, but I've occasionally had similar complaints about Qt, which I've used a lot more. It uses a signal-slot paradigm where you basically say "when this event is triggered, this function gets called" (in a callback-like, but somewhat more powerful and flexible fashion). It's great for a lot of things, but makes it hard to read and reason about synchronous operations that have to be fit into this form.

Ultimately, though, people cope with it and write working software with it. I imagine it's the same with Node :-).

Re:“Node.js is one of the worst things to happen to the soft... by s_kilk on Tuesday August 23, 2016 @05:26AM (#12342412)
> What is async in JS and Node (unless you use the sync alternatives) is any kind of IO (disk, network). That is also async for the CPU.

The same statement is true of Erlang and Elixir, yet they manage to handle non-blocking async code without either of the daft callback/promise code styles of Javascript.

Even within the confines of async-capable languages, Node is one of the worse options.

Re:“Node.js is one of the worst things to happen to the soft... by the_duke on Tuesday August 23, 2016 @06:42AM (#12342654)
I agree that CSP as in Erlang/Elixir or Go is way better for handling async behaviour.
Re:“Node.js is one of the worst things to happen to the soft... by mateuszf on Tuesday August 23, 2016 @03:19PM (#12346642)
Or clojure, whose flavor compiles down to JS.
Re:“Node.js is one of the worst things to happen to the soft... by tigershark on Tuesday August 23, 2016 @03:42AM (#12342034)
As I said in my previous post, a CPU that doesn't respect barriers and reorders the instructions anyway is a bugged CPU, not a modern one.

If I need the database result before doing anything else then my code is inherently synchronous and it has an hard dependency on the database result. In that context making the DB call asynchronous and waiting for it is more complex than making a synchronous call and proceeding with the normal program flow. The most natural way to write an algorithm is to have everything synchronous and have a "simil-synchronisation" of the asynchronous program flow using an "async monad". And sadly I normally use C# 4.0 that still doesn't have the async construct borrowed from F#, so I'm stuck with tasks and continuations.

Re:“Node.js is one of the worst things to happen to the soft... by talles on Tuesday August 23, 2016 @09:08AM (#12343386)
> And sadly I normally use C# 4.0 that still doesn't have the async construct borrowed from F#, so I'm stuck with tasks and continuations.

Do you know that the async/await constructs are just sugar for the Task class you have access on C# 4.0?

Re:“Node.js is one of the worst things to happen to the soft... by tigershark on Tuesday August 23, 2016 @06:33PM (#12348321)
The C# implementation is a wrap/unwrap of the Task class, in F# it is using computation expressions to lift the synchronous code to an Async monad. The async construct is much easier to reason about, especially with nested tasks.
Re:“Node.js is one of the worst things to happen to the soft... by marssaxman on Tuesday August 23, 2016 @01:49AM (#12341672)
We are talking about different kinds of asynchrony. Yes, the CPU marches to the beat of a single drum, but even that "advance" signal can be seen as an action taken in response to an external stimulus: the voltage change incoming from the clock crystal.
Re:“Node.js is one of the worst things to happen to the soft... by tigershark on Tuesday August 23, 2016 @02:00AM (#12341709)
Nope. When the clock signal is received it means that the components in the critical path have sent the output in the destination, and so the state of the chip is coherent and it starts another step, synchronous across all the chip as the previous. Using your definition then I can say that a block of procedural code that starts on a single thread as soon as an HTTP request is received, processing it and blocking the thread until the end, is asynchronous because it is an action taken in response to an external stimulus, to use your own words. But it is simply not true. The mechanism that dispatches the call to start the subroutine is asynchronous, the code itself is synchronous.
Re:“Node.js is one of the worst things to happen to the soft... by marssaxman on Tuesday August 23, 2016 @02:14AM (#12341748)
I'll take your word for it; we are moving into architecture that sits below my experience now. But we are also below the level of abstraction relevant to the original question, which concerned the design of an API used for communicating with hardware outside the CPU.
Re:“Node.js is one of the worst things to happen to the soft... by Jach on Tuesday August 23, 2016 @02:54AM (#12341877)
Any system that shares a clock is synchronous. Two common communication methods for hardware are SPI, which has a shared clock, and is hence synchronous (https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/SP...) and UART, which has no shared clock, indeed it can be implemented with a single wire, and is hence asynchronous. Internally the receiver's clock needs to be fast enough to correctly sample data being transmitted to it. (Ed: or I guess you might be able to do something clever with an async circuit fed directly by the signaling line.)

Communicating with hardware outside the CPU doesn't have to be synchronous or asynchronous, it's a design decision depending on what you're trying to accomplish. Languages that don't support both methods are missing half of the design space.

Re:“Node.js is one of the worst things to happen to the soft... by boomlinde on Tuesday August 23, 2016 @07:20AM (#12342787)
I believe it's relevant to point out that most CPUs are synchronous in response to the claim that "Processors really are fundamentally asynchronous."
Re:“Node.js is one of the worst things to happen to the soft... by marssaxman on Tuesday August 23, 2016 @12:45PM (#12345118)
It does seem like I could have phrased that differently, since a number of people have taken it to mean something rather different than I intended.
Re:“Node.js is one of the worst things to happen to the soft... by cookiecaper on Tuesday August 23, 2016 @04:00AM (#12342086)
Isn't this essentially saying that events need a catalyst, there needs to be some external stimulus for something to occur? That's fine. Synchronous programs have that stimulus. It's called "user action". The program, like the CPU, waits until the entity running the show tells it it would like something to happen. Then it does that something in the order specified.

So yes, the CPU waits for the program to ask for some work, just like the program waits for the user to ask for some work. When that work is requested, the program is executed synchronously. I really don't understand how this argument supports the mess that is Node.js.

Re:“Node.js is one of the worst things to happen to the soft... by taneq on Tuesday August 23, 2016 @03:38AM (#12342021)
Well, CPUs do use out-of-order execution to optimise some things: https://en.wikipedia.org/wiki/Out-of-order_execution

But, crucially, they can only do it when it still behaves as if they're operating in-order.

Either way, I agree that CPUs are explicitly synchronous in terms of how they work internally and how they interact with the outside world.

Re:“Node.js is one of the worst things to happen to the soft... by tiagobertolo on Wednesday August 24, 2016 @07:16PM (#12355951)
What really happens is in fact very similar to what happens in the javascript event loop.

The processor processes instructions one after another, synchronously, but whenever he has to use something external he makes the call and continues his normal life until the interruption triggers.

The javascript event loop consists in placing function calls in stack and using WEB api calls asynchronously.