News for hackers. Stuff that matters.
Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by cogman10 on Monday May 02, 2022 @02:55PM (#31239281)
JS has an uber lock. JS has a strict requirement that only one thread can run javascript code. That means that whenever you get to that `await` block, you've effectively established a whole application lock which prevents other awaits from proceeding (until your code yields). In otherwords JS isn't lock free, it's got 1 really big lock that covers all user code.

If you really wanted to, you could simulate this behavior in threaded languages by having a global lock you acquire before you run anything.

It's the python GIL problem [1]

Languages like kotlin, rust, C++, or C# with async/await MUST concern themselves with locks because they don't acquire whole application locks anytime they run a sliver of code.

[1] https://realpython.com/python-gil/

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by SemanticStrengh on Monday May 02, 2022 @03:15PM (#31239528)
Great explanation in general except web workers are a thing and sharedarraybuffer allow shared memory between them. Thats why js has atomics. Although while js can have conçurent races via shared memory, it doesn't expose locks IIRC? If so that's bad
Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by cogman10 on Monday May 02, 2022 @03:23PM (#31239610)
Web workers are certainly weird... and it's generally best to ignore them :D

But if you don't, then yeah, you sort of run right into the need for an actual lock since you can have shared memory in concurrently executing interpreters. Entire: Web Lock [1] Specifically designed to resolve this issue.

In typical JS programming this is not an issue, but if you are trying to really abuse that javascript VM then this is the way you'd do it.

[1] https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_A...

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by SemanticStrengh on Monday May 02, 2022 @03:31PM (#31239728)
Oh thank u web locks are a thing then. Well in all seriousness there are many valid uses for web workers, e.g. For offline processing of localstorage