ymd-h / exodide

Build & Install custom C/C++ extension package for Pyodide
MIT License
21 stars 4 forks source link

Error when creating whl with std::thread code #35

Open paulgheorghecristian opened 7 months ago

paulgheorghecristian commented 7 months ago

Helo,

When I try creating threads in CPP, creating a wheel and importing in pyodide 0.21.0, I get this:

index.js:1 CppException std::__2::system_error: thread constructor failed: Resource temporarily unavailable

This is the program:

void run_threads() {
    std::thread t([&](){
        for (int i = 0; i < 10; i++) {
            std::cout << "i:" << i << std::endl;
        }
    });
    std::thread t2([&](){
        for (int j = 10; j < 20; j++) {
            std::cout << "j:" << j << std::endl;
        }       
    });

    t.join();
    t2.join();
}

PYBIND11_MODULE(example, m){
  m.def("run_threads", &run_threads, "Run threads");
}

I know there are some issues with running multithreaded code, is there any way I can make this work?

Thanks!

ymd-h commented 7 months ago

Hi, @paulgheorghecristian

In exodide, -pthread option is explicitly disabled. As far as I remember, the option produced compile error (at least at that time).

https://github.com/ymd-h/exodide/blob/master/design.md#build-details https://github.com/ymd-h/exodide/blob/b5232240edcc436acc181d1b708dd801dda6af87/exodide/build.py#L127-L141

With my quick search, Pyodide hasn't supported thread yet, and I have no idea whether we could support thread separately. https://github.com/pyodide/pyodide/issues/237