最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Minimal working example for emscripten webworker - Stack Overflow

programmeradmin5浏览0评论

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.

Share Improve this question edited Aug 29, 2015 at 22:07 Alexander asked Aug 29, 2015 at 22:02 AlexanderAlexander 1,3292 gold badges13 silver badges32 bronze badges 2
  • 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
Add a ment  | 

1 Answer 1

Reset to default 16

Compile with BUILD_AS_WORKER flag.

emcc -std=c++11 worker.cpp -s EXPORTED_FUNCTIONS="['_one']" -s BUILD_AS_WORKER=1 -o worker.js
发布评论

评论列表(0)

  1. 暂无评论