Ive recently started using emscripten and have been stuck for several days
trying to perform HTTP requests on threads. i originally tried calling to
js xhr and then fetch apis, but couldnt get that to work, so then tried the
emscripten_fetch api.
my build flags
-std=c++20
-pthread
-s USE_PTHREADS=1
-s ALLOW_BLOCKING_ON_MAIN_THREAD=1
-s PTHREAD_POOL_SIZE=8
-s FETCH=1
Below is an isolated code example which hangs on emscripten_fetch. the
code is identical to the example on official docs (plus a short timeout
specified but that makes no difference). if i just do a long
emscripten_sleep it works ok, so threads in general appear to be ok.
Can anyone shed any light on this?
Thanks
---
#include <emscripten.h>
#include <emscripten/fetch.h>
#include <thread>
#include <chrono>
static_assert(__EMSCRIPTEN_PTHREADS__ , "no threads");
void Print(const char *str)
{
emscripten_log(EM_LOG_CONSOLE, "%s", str);
}
int main()
{
Print("main");
std::atomic <bool> completed = false;
std::thread task([&completed]()
{
Print("http on bg thread");
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.timeoutMSecs = 1;
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY |
EMSCRIPTEN_FETCH_SYNCHRONOUS;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "testfile.json"); //
Blocks here until the operation is complete.
if (fetch->status == 200)
{
Print("downloaded ok");
}
else
{
Print("download failed");
}
emscripten_fetch_close(fetch);
completed.store(true);
Print("/http on bg thread");
});
while (!completed.load())
{
Print("waiting for bg thread");
emscripten_sleep(50);
}
task.join();
Print("/main");
return 0;
}
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/emscripten-discuss/04f2c509-1b2f-48d7-ab7a-9728103b8e3en%40googlegroups.com.