gpt4 book ai didi

pthreads - 线程使用 emscripten

转载 作者:行者123 更新时间:2023-12-04 13:49:45 25 4
gpt4 key购买 nike

我正在尝试将线程与 Emscripten 一起使用,但我不明白它是如何工作的。我已经阅读了一些关于网络 worker 的文字,但我不确定是否理解。

当我查看“tests”文件夹时,I can see pthread stuff .

我正在使用“std::thread”并收到以下错误:

unresolved symbol: pthread_create

我是否必须使用网络 worker 而不是默认线程?

谢谢!

最佳答案

正在添加对 pthread 的支持,并且已经可以通过一些设置来使用。由于 std::thread 在底层使用 pthread,因此您也可以使用它。见 this discussion想要查询更多的信息。

我必须做的:

  • 使用较新的 emscripten(我正在使用 1.34.1 进行测试)
  • 安装 Firefox Nightly
  • 启用标志 USE_PTHREADS
  • 请注意,这是实验性的,有些东西很挑剔

  • 我在编写实际运行的 pthread 示例时遇到了麻烦,但这里是使用 std::thread 的代码,它演示了对我有用的基本功能:
    // main.cpp

    #include <thread>
    #include <iostream>

    void func()
    {
    std::cout << "I'm a thread!\n";
    }

    int main()
    {
    std::thread test1(func);
    std::thread test2(func);
    std::thread test3(func);

    // join seems to lock up the browser
    //test1.join();
    //test2.join();
    //test3.join();
    }

    我已经能够在一个更大的项目中使用线程(这里的帖子很大!),所以它们是可行的。恐怕它们并没有那么快,尽管这可能会随着时间的推移而改善。

    要构建它:

    emcc main.cpp -o main.html -s USE_PTHREADS=1 --std=c++11



    Firefox Nightly 42.0a1 (2015-07-16) 中的输出:

    Preallocating 1 workers for a pthread spawn pool.
    Preallocating 1 workers for a pthread spawn pool.
    Preallocating 1 workers for a pthread spawn pool.
    I'm a thread!
    I'm a thread!
    I'm a thread!

    关于pthreads - 线程使用 emscripten,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473959/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com