Chapter 6. Multithreaded Patterns

The JavaScript APIs that expose multithreading are, on their own, really quite basic with the functionality they provide. As you saw in Chapter 4, the purpose of the SharedArrayBuffer is to store a raw, binary representation of data. Even Chapter 5 continued this pattern with the Atomics object, exposing rather primitive methods for coordinating or modifying a handful of bytes at a time.

Just looking at such abstract and low-level APIs can make it difficult to see the big picture, or what these APIs can really be used for. It’s admittedly difficult to take these concepts and convert them into something that is genuinely useful for an application. That’s what this chapter is for.

This chapter contains popular design patterns for implementing multithreaded functionality inside an application. These design patterns take inspiration from the past, as each of them existed long before JavaScript was even invented. Though working demos of them are likely available in many forms, such as C++ textbooks, translating them for use with JavaScript isn’t always straightforward.

By examining these patterns you’ll get a much better feel for how the applications you develop can benefit from multithreading.

Thread Pool

The thread pool is a very popular pattern that is used in most multithreaded applications in some form or another. Essentially, a thread pool is a collection of homogeneous worker threads that are each capable of carrying out CPU-intensive ...

Get Multithreaded JavaScript now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.