首页 > 解决方案 > 在 Emscripten 中,通过活动循环等待生成的线程失败。我该如何修改它?

问题描述

我尝试通过 Emscripten 将一些开源 C++ 项目转换为 WebAssembly。关于多线程,该项目使用带有条件变量的活动循环来等待线程的产生。

void spawnMethod() {
    numServerThreadsStartingUp = numThreads;
    for(int i = 0; i<numThreads; i++) {
        std::thread* thread = new std::thread(&aFunction,);
        serverThreads.push_back(thread);
    }
    unique_lock<std::mutex> lock(bufferMutex);
    while(numServerThreadsStartingUp > 0)
        mainThreadWaitingForSpawn.wait(lock);
}

void aFunction() {
    {
        lock_guard<std::mutex> lock(bufferMutex);
        numServerThreadsStartingUp--;
        if(numServerThreadsStartingUp <= 0)
            mainThreadWaitingForSpawn.notify_all();
    }

}

在 C++ 中,它运行良好,但在 Emscripten 中它不起作用。永远不会产生子线程。我认为您需要退出作为 Web Worker 调用的 spawnMethod 以生成其他线程。我的理解对吗?什么是解决方法?

谢谢你。

标签: emscripten

解决方案


推荐阅读