The nature of JavaScript is to be single threaded. This is reflected not only in libraries and applications, but also in online forum posts, books, and online documentation. Thanks to recent advancements in the platform—such as with web workers in the browser, worker_threads in Node.js, and the Atom
Multithreaded JavaScript: Concurrency Beyond the Event Loop
✍ Scribed by Thomas Hunter II, Bryan English
- Publisher
- O'Reilly Media
- Year
- 2021
- Tongue
- English
- Leaves
- 214
- Edition
- 1
- Category
- Library
No coin nor oath required. For personal study only.
✦ Synopsis
The nature of JavaScript is to be single threaded. This is reflected not only in libraries and applications, but also in online forum posts, books, and online documentation. Thanks to recent advancements in the platform—such as with web workers in the browser, worker_threads in Node.js, and the Atomics and SharedArrayBuffer objects—JavaScript engineers are able to build multi-threaded applications. These features will go down as being the biggest paradigm shift for the world's most popular programming language.
Multithreaded JavaScript explores the various features that JavaScript runtimes have at their disposal for implementing multithreaded programming, using a spectrum of API reference material and high level programming patterns.
• Learn what multithreaded programming is and how you can benefit from it
• Understand the differences between a dedicated worker, a shared worker, and a service worker
• Identify when and when not to use threads in an application
• Orchestrate communication between threads by leveraging the Atomics object
• Understand both the gains and pitfalls of using shared memory
• Benchmark performance to learn when you'll benefit from multiple threads
✦ Table of Contents
Cover
Copyright
Table of Contents
Foreword
Preface
Target Audience
Goals
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Introduction
What Are Threads?
Concurrency Versus Parallelism
Single-Threaded JavaScript
Hidden Threads
Threads in C: Get Rich with Happycoin
With Only the Main Thread
With Four Worker Threads
Chapter 2. Browsers
Dedicated Workers
Dedicated Worker Hello World
Advanced Dedicated Worker Usage
Shared Workers
Shared Worker Hello World
Advanced Shared Worker Usage
Service Workers
Service Worker Hello World
Advanced Service Worker Concepts
Message Passing Abstractions
The RPC Pattern
The Command Dispatcher Pattern
Putting It All Together
Chapter 3. Node.js
Before We Had Threads
The worker_threads Module
workerData
MessagePort
Happycoin: Revisited
With Only the Main Thread
With Four Worker Threads
Worker Pools with Piscina
A Pool Full of Happycoins
Chapter 4. Shared Memory
Intro to Shared Memory
Shared Memory in the Browser
Shared Memory in Node.js
SharedArrayBuffer and TypedArrays
Atomic Methods for Data Manipulation
Atomics.add()
Atomics.and()
Atomics.compareExchange()
Atomics.exchange()
Atomics.isLockFree()
Atomics.load()
Atomics.or()
Atomics.store()
Atomics.sub()
Atomics.xor()
Atomicity Concerns
Data Serialization
Booleans
Strings
Objects
Chapter 5. Advanced Shared Memory
Atomic Methods for Coordination
Atomics.wait()
Atomics.notify()
Atomics.waitAsync()
Timing and Nondeterminism
Example of Nondeterminism
Detecting Thread Preparedness
Example Application: Conway’s Game of Life
Single-Threaded Game of Life
Multithreaded Game of Life
Atomics and Events
Chapter 6. Multithreaded Patterns
Thread Pool
Pool Size
Dispatch Strategies
Example Implementation
Mutex: A Basic Lock
Streaming Data with Ring Buffers
Actor Model
Pattern Nuances
Relating to JavaScript
Example Implementation
Chapter 7. WebAssembly
Your First WebAssembly
Atomic Operations in WebAssembly
Compiling C Programs to WebAssembly with Emscripten
Other WebAssembly Compilers
AssemblyScript
Happycoin in AssemblyScript
Chapter 8. Analysis
When Not to Use
Low Memory Constraints
Low Core Count
Containers Versus Threads
When to Use
Summary of Caveats
Appendix A. Structured Clone Algorithm
Index
About the Authors
Colophon
✦ Subjects
Multithreading; JavaScript; Concurrency; Memory Management; Node.js; WebAssembly
📜 SIMILAR VOLUMES
Traditionally, JavaScript has been a single-threaded language. Thanks to recent advancements in the language—such as the Atomics and SharedArrayBuffers objects and Web Workers in the browser—JavaScript is now a multi-threaded language. These features will go down as being the biggest paradigm shift
After you have programmed in Javascript for a while, you begin to wonder how things work. Some elusive answers are clarified in this short ebook. The presentation is in the style of the author's well-received ebook on Haskell, “From Simple IO to Monad Transformers”. This style was
After you have programmed in Javascript for a while, you begin to wonder how things work. Some elusive answers are clarified in this short ebook. The presentation is in the style of the author's well-received ebook on Haskell, “From Simple IO to Monad Transformers”. This style was
C++ Concurrency in Action is a reference and guide to the new C++ 11 Standard for experienced C++ programmers as well as those who have never written multithreaded code. This book will show you how to write robust multithreaded applications in C++ while avoiding many common pitfalls.<br>Multiple pro
C++ Concurrency in Action is a reference and guide to the new C++ 11 Standard for experienced C++ programmers as well as those who have never written multithreaded code. This book will show you how to write robust multithreaded applications in C++ while avoiding many common pitfalls.<br>Multiple pro