54; int bg_val = 42; pthread_t bg_thread; // Create the background thread if (pthread_create(&bg_thread, NULL, bg_func, &bg_val)) { perror("Thread create failed"); return 1; } // Calculate on the foreground thread fg_val = fibonacci(fg_val); // Wait for background thread to finish if (pthread_join(bg_thread, NULL)) { perror("Thread join failed"); return 2; } // Show the result from background and foreground threads printf("Fib(42) is %d, Fib(54) is %d\n", bg_val, fg_val); return 0; }
extern "C" fn main() { let normal = 54; let background = 42; let handler = std::thread::spawn(move || fibonacci(background)); let normal = fibonacci(normal); let background = handler.join().expect("Unsupported Thread"); unsafe { console_log(normal as f64); console_log(background as f64); } }
は Atomics のみの提案だった ◦ ブラウザの SharedArrayBuffer は扱えるが Web Workers API は扱えない • Emscripten の場合 JavaScript のグルーコード内でスレッドを作っている https://git.io/JeVk0 11 Multithreading WebAssembly by Rust // Allow HTML module to configure the location where the 'worker.js' file will be loaded from, // via Module.locateFile() function. If not specified, then the default URL 'worker.js' relative // to the main html file is loaded. pthreadMainJs = locateFile(pthreadMainJs); var newWorkers = []; for (var i = 0; i < numWorkers; ++i) { newWorkers.push(new Worker(pthreadMainJs)); } return newWorkers;