-
I'm wondering how to pass the result of a detached thread back to the main thread. My naive solution looks like this: #include <thread>
#include <future>
#include <iostream>
#include <chrono>
#include <emscripten.h>
#include <emscripten/bind.h>
void compute_stuff() {
std::thread thread([] {
std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
EM_ASM(postMessage("Done!")); // signal the main thread that we are done
});
thread.detach();
}
EMSCRIPTEN_BINDINGS(Loader) {
emscripten::function("compute_stuff", &compute_stuff);
} var webworker = new Worker("ThreadTest.worker.js");
webworker.onmessage = (e) => {
console.log("Received message: " + e.data);
};
ThreadTest().then((module) => {
module.compute_stuff();
}); Compiled with
However, this doesn't work because emscripten already uses the message channel to control the WebWorker (it will print "worker sent an unknown command undefined"). The only solution I found so far, is using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I change is currently in flight that allows you to use the postMessage mechanism to communicate with the main thread: #16239. Hopefully that will solve your issue? |
Beta Was this translation helpful? Give feedback.
I change is currently in flight that allows you to use the postMessage mechanism to communicate with the main thread: #16239. Hopefully that will solve your issue?