First thing is to remember to also compile with -s NO_EXIT_RUNTIME=1,
since once main() exits, it will tear down the application runtime,
along with all threads, so the linker flag will keep it running after
exiting main. I wonder if that already will solve the issue?

Another note is that C++11 threading is known to have some bugs, the
pthread_create() API is robust and unit tested. (If someone knows of a
good open source unit test suite for C++11 threading, I'm all ears)

2017-08-09 21:37 GMT+03:00 Scott Watson <wswat...@gmail.com>:
> I am having trouble porting our application to asm.js.  We have a class that
> handles HTTP GETs for us, that I am trying to port using the
> emscripten_fetch api.
>
> I can only get the asynchronous fetch to work, and only when called on the
> main thread.  I can't get synchronous to work at all.  Using the sample code
> from:
> https://kripken.github.io/emscripten-site/docs/api_reference/fetch.html, I
> built this.
>
>     // main.cpp
>
>     #include <thread>
>     #include <iostream>
>
>     #include <emscripten/fetch.h>
>
>     void downloadSucceeded(emscripten_fetch_t *fetch) {
>       printf("Finished asynchronously downloading %llu bytes from URL
> %s.\n", fetch->numBytes, fetch->url);
>       // The data is now available at fetch->data[0] through
> fetch->data[fetch->numBytes-1];
>       emscripten_fetch_close(fetch); // Free data associated with the fetch.
>     }
>
>     void downloadFailed(emscripten_fetch_t *fetch) {
>       printf("Downloading %s asynchronously failed, HTTP failure status
> code: %d.\n", fetch->url, fetch->status);
>       //emscripten_fetch_close(fetch); // Also free data on failure.
>     }
>
>     void worker()
>     {
>       emscripten_fetch_attr_t attr;
>
>       emscripten_fetch_attr_init(&attr);
>
>       strcpy(attr.requestMethod, "GET");
>
>       attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
>       attr.onsuccess = downloadSucceeded;
>       attr.onerror = downloadFailed;
>
>       emscripten_fetch(&attr, "http://localhost/VOD/nangu/00000041.ts";); //
> Blocks here until the operation is complete.
>     }
>
>     int main()
>     {
>         // worker();
>         std::thread test1(worker);
>     }
>
> I am building it using:
>
>     emcc main.cpp -o $(OUTPUT_FOLDER)/thread_test.html -s USE_PTHREADS=1 -s
> PTHREAD_POOL_SIZE=5 -s FETCH=1
>
> My downloadFailed method is being called with a 'fetch->status' of 0.  If I
> call the 'worker()' method directly I get the expected behavior.
>
> I am tearing my hair out here.  I have googled everything I can think of to
> no avail.
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to emscripten-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to emscripten-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to