emscripten - 在 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 以生成其他线程。我的理解对吗?什么是解决方法?
谢谢你。
解决方案
推荐阅读
- html - 所需的 html 仅适用于一种形式
- javascript - undefined 不是对象 React Native
- ruby - 存储在 CSV 文件中 - 红宝石分隔符
- android - 使用 WeakReference Android 避免内存泄漏
- angular - 使用 ngx-translate 更改 Angular 5 中语言更改的页面 URL
- ios - Swift 4:具有可选值的对象的 JsonSerialization 失败
- javascript - json登录会话错误
- excel - Excel 格式/基于最后一个日期和相等的单元格值突出显示行
- vba - 根据标准找到另一个单元格编号后除以一个单元格编号
- ios - 尝试解开领域对象时出错