diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-10 12:02:10 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-10 12:02:10 +0200 |
| commit | 4ea98362c39d63bbfaef28aa25a1e630e01c5611 (patch) | |
| tree | 58325878046ec59b22cc0012497f220cc04762eb | |
| parent | 18132bf9c9755a7de41a8c89dc6b02409a629cd5 (diff) | |
| download | emscripten-tutorial-4ea98362c39d63bbfaef28aa25a1e630e01c5611.tar.gz emscripten-tutorial-4ea98362c39d63bbfaef28aa25a1e630e01c5611.zip | |
| -rwxr-xr-x | 03_threads/build-native.sh | 4 | ||||
| -rw-r--r-- | 03_threads/native.c | 10 | ||||
| -rw-r--r-- | 03_threads/program.mjs | 6 | ||||
| -rw-r--r-- | README.md | 5 |
4 files changed, 23 insertions, 2 deletions
diff --git a/03_threads/build-native.sh b/03_threads/build-native.sh new file mode 100755 index 0000000..251e0a0 --- /dev/null +++ b/03_threads/build-native.sh | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | mkdir -p build | ||
| 4 | cc -o build/native primes.c native.c | ||
diff --git a/03_threads/native.c b/03_threads/native.c new file mode 100644 index 0000000..4a3d4fa --- /dev/null +++ b/03_threads/native.c | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | |||
| 3 | int primes_in_range(int, int); | ||
| 4 | |||
| 5 | int main() { | ||
| 6 | int low = 1; | ||
| 7 | int high = 10000000; | ||
| 8 | int result = primes_in_range(low, high); | ||
| 9 | printf("There are %d primes between %d and %d\n", result, low, high); | ||
| 10 | } | ||
diff --git a/03_threads/program.mjs b/03_threads/program.mjs index db0a2c6..3bdb4ea 100644 --- a/03_threads/program.mjs +++ b/03_threads/program.mjs | |||
| @@ -2,5 +2,7 @@ import Primes from "./build/primes.mjs" | |||
| 2 | 2 | ||
| 3 | var primes = await Primes(); | 3 | var primes = await Primes(); |
| 4 | 4 | ||
| 5 | const count = primes._primes_in_range(1, 100); | 5 | var low = 1; |
| 6 | console.log("There are " + count + " primes between 1 and 100"); | 6 | var high = 10000000; |
| 7 | const count = primes._primes_in_range(low, high); | ||
| 8 | console.log("There are " + count + " primes between " + low + " and " + high); | ||
| @@ -67,6 +67,11 @@ In `03_threads` we build a more complicated example based on | |||
| 67 | example, the web server has to be configured to provide the correct | 67 | example, the web server has to be configured to provide the correct |
| 68 | `Cross-Origin-*` headers, see `03_threads/run-server.sh` for details. | 68 | `Cross-Origin-*` headers, see `03_threads/run-server.sh` for details. |
| 69 | 69 | ||
| 70 | There is also a piece of code meant to run this more complicated library | ||
| 71 | function natively (`native.c`). It can be used, for example, to compare | ||
| 72 | the performance of native code vs WASM running in Node.js. You can | ||
| 73 | build it with `./build-native.sh` and run it with `./build/native`. | ||
| 74 | |||
| 70 | ## 4. Don't block the main thread | 75 | ## 4. Don't block the main thread |
| 71 | 76 | ||
| 72 | In `04_no_block` we avoid our calculations blocking the main browser | 77 | In `04_no_block` we avoid our calculations blocking the main browser |
