I'm trying to build a basic webworker example in C++ with emscripten. The API looks very simple, but I can't get it working. I actually wanted to implement this functionality in my project, but after failing tried to make a minimal example and it also doesn't work.
I have main.cpp:
#include <emscripten/emscripten.h>
#include <emscripten/bind.h>
#include <iostream>
namespace e = emscripten;
int counter = 0;
void cback(char* data, int size, void* arg) {
std::cout << "Callback" << std::endl;
counter++;
}
void loop() {
std::cout << "Counter: " << counter << std::endl;
}
int main() {
std::cout << "Main func." << std::endl;
worker_handle worker = emscripten_create_worker("worker.js");
emscripten_call_worker(worker, "one", 0, 0, cback, (void*)42);
emscripten_set_main_loop(loop, 2, true);
return 0;
}
and worker.cpp:
#include <iostream>
#include <emscripten/emscripten.h>
extern "C" {
void one(char* data, int size) {
for(int i=0; i<10; i++) {
std::cout << "Worker" << std::endl;
emscripten_worker_respond_provisionally(0, 0);
}
emscripten_worker_respond(0, 0);
}
}
piled via
emcc -std=c++11 main.cpp -o main.js
emcc -std=c++11 worker.cpp -s EXPORTED_FUNCTIONS="['_one']" -o worker.js
and a simple js load via <script>
tag on the html side.
Main loads and starts, outputs Main func.
and then worker js is downloaded. But neither Worker
nor Callback
is outputed. No errors reported.
I'm trying to build a basic webworker example in C++ with emscripten. The API looks very simple, but I can't get it working. I actually wanted to implement this functionality in my project, but after failing tried to make a minimal example and it also doesn't work.
I have main.cpp:
#include <emscripten/emscripten.h>
#include <emscripten/bind.h>
#include <iostream>
namespace e = emscripten;
int counter = 0;
void cback(char* data, int size, void* arg) {
std::cout << "Callback" << std::endl;
counter++;
}
void loop() {
std::cout << "Counter: " << counter << std::endl;
}
int main() {
std::cout << "Main func." << std::endl;
worker_handle worker = emscripten_create_worker("worker.js");
emscripten_call_worker(worker, "one", 0, 0, cback, (void*)42);
emscripten_set_main_loop(loop, 2, true);
return 0;
}
and worker.cpp:
#include <iostream>
#include <emscripten/emscripten.h>
extern "C" {
void one(char* data, int size) {
for(int i=0; i<10; i++) {
std::cout << "Worker" << std::endl;
emscripten_worker_respond_provisionally(0, 0);
}
emscripten_worker_respond(0, 0);
}
}
piled via
emcc -std=c++11 main.cpp -o main.js
emcc -std=c++11 worker.cpp -s EXPORTED_FUNCTIONS="['_one']" -o worker.js
and a simple js load via <script>
tag on the html side.
Main loads and starts, outputs Main func.
and then worker js is downloaded. But neither Worker
nor Callback
is outputed. No errors reported.
- Does running em++ instead of emcc make a difference? – Michal Charemza Commented Aug 30, 2015 at 10:21
-
1
Why do you import
emscripten/bind.h
? – Frederik Krautwald Commented May 30, 2017 at 18:00
1 Answer
Reset to default 16Compile with BUILD_AS_WORKER
flag.
emcc -std=c++11 worker.cpp -s EXPORTED_FUNCTIONS="['_one']" -s BUILD_AS_WORKER=1 -o worker.js