Skip to content

Commit

Permalink
Update Cheerp web workers tutorial (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
DutChen18 authored Aug 6, 2024
1 parent 9244c7d commit e458e84
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions sites/cheerp/src/content/docs/13-tutorials/04-webworkers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
title: Threading with Web Workers
---

# WebWorkers vs. PThreads
## 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?
## 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!
## WebWorkers in Cheerp!

Cheerp is designed to give you full access to all browser APIs, including WebWorkers.

## Using WebWorkers with Cheerp
### 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 ported from http://www.html5rocks.com/en/tutorials/workers/basics/.
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.

```cpp title="cheerpWorkerHost.cpp"
// cheerpWorkerHost.cpp: Code to include in the HTML page
Expand All @@ -33,26 +33,25 @@ using namespace client;
void webMain()
{
Worker* w = new Worker("cheerpWorker.js");
w->addEventListener("message", cheerp::Callback([](MessageEvent* e) {
console.log((String*)(e->get_data())); }));
w->postMessage("Hello World");

w->addEventListener("message", [](Object* e) {
String* data = e->cast<MessageEvent<String*>*>()->get_data();
console.log((new String("worker message: "))->concat(data));
});
}
```

```cpp title="cheerpWorker.cpp"
// cheerpWorker.cpp: Code that runs inside the worker
#include <cheerp/clientlib.h>
#include <cheerp/client.h>

using namespace client;

[[cheerp::genericjs]]
void webMain()
{
addEventListener("message", cheerp::Callback([](MessageEvent* e) {
postMessage(e->get_data());
postMessage(e->get_data());
}));
postMessage(new String("hello, world!"));
postMessage(new String("hello, world!"));
}
```

Expand Down

0 comments on commit e458e84

Please sign in to comment.