Theme using emscripten

I am trying to use thread with Emscripten and I do not understand how it works. I read some text about web workers, and I'm not sure I understood.

When I look in the tests folder, I see the pthread stuff .

I use "std :: thread" and got the following error:

unresolved symbol: pthread_create 

Should I use web workers instead of the default thread?

Thanks!

+5
source share
2 answers

Pthread support is added and can already be used with a small amount of settings. Since std :: thread uses pthread under the hood, you can also use it. See this discussion for more information.

What I had to do:

  • Use new emscripten (I am testing with 1.34.1)
  • Install Firefox at night
  • Enable flag USE_PTHREADS
  • Remember that this is experimental, and some things are finishing in nature.

I had a problem writing a pthread example that actually ran, but here's the code using std :: thread, which demonstrates the basic functionality that worked for me:

 // 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(); } 

I managed to use threads in a larger project (for a larger one for posting here!), So they are viable. I’m afraid it’s not so fast, although it may improve over time.

To build it:

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

Output in Firefox Nightly 42.0a1 (2015-07-16):

Preliminary allocation of 1 worker for pthread breed pool.
Preliminary allocation of 1 employee for the pthread pool pool.
Preliminary allocation of 1 employee for the pthread pool pool.
I am a thread! I am a thread! I am a thread!

+9
source

Unfortunately, multithreaded code cannot be compiled using Emscripten. Web site workers can be calculated at the same time, but they cannot maintain general state and therefore cannot replace threads.

See: http://kripken.imtqy.com/emscripten-site/docs/porting/guidelines/portability_guidelines.html

Change Since others have a pointer, Emscripten is moving forward with my original answer and now has experimental support for pthreads (and most likely soon for C ++ inline threads) https://groups.google.com/forum/#!topic/emscripten -discuss / gQQRjajQ6iY

+3
source

Source: https://habr.com/ru/post/1209024/


All Articles