Threading with Web Workers
WebWorkers vs. PThreads
PThreads is the main threading API on posix platforms, such as Linux. It makes it possible to run concurrent threads of execution that have the same view of memory (i.e. they run in the same address space).
WebWorkers are a message based concurrency API for the Web platform. A WebWorker is a โthreadโ which executes a JavaScript file which is completely independent and isolated from the main page code and other WebWorkers. Since the script inside a WebWorker has an isolated view of memory a WebWorker is more similar to a native process, than to a native thread.
PThread in Cheerp?
Cheerp does not support yet PThread APIs, but itโs a work in progress. Please get in touch should you want to be kept informed.
JavaScript, compiled or hand-written, miss feature that allow actual threading, while the Web Assembly part of the program could benefit from multithreading. Browser support for now itโs only experimental, but that will change in the near future.
WebWorkers in Cheerp!
Cheerp is designed to give you full access to all browser APIs, including WebWorkers.
Using WebWorkers with Cheerp
You can use Cheerp to generate JavaScript running in the Worker, JavaScript in the main page that run a Worker, or both. Below you find a trivial example.
You can build these files using the following commands:
For your convenience here is the needed HTML to execute the code
Start http-server
, navigate to the right html, and check the console log.
You should see a greeting echoed back twice through a Worker.