Maratyszcza / pthreadpool

Portable (POSIX/Windows/Emscripten) thread pool for C/C++
BSD 2-Clause "Simplified" License
342 stars 132 forks source link

Implementation in EMSCRIPTEN #16

Closed XU-YaoKun closed 3 years ago

XU-YaoKun commented 3 years ago

when compiling in EMSCRIPTEN environment, there is only shim.c got compiled.

This is the concerning CMakeLists.txt part,

IF(EMSCRIPTEN)
  LIST(APPEND PTHREADPOOL_SRCS src/shim.c)
ELSE()
  LIST(APPEND PTHREADPOOL_SRCS src/portable-api.c src/memory.c)
  IF(APPLE AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "gcd"))
    LIST(APPEND PTHREADPOOL_SRCS src/gcd.c)
  ELSEIF(CMAKE_SYSTEM_NAME MATCHES "^(Windows|CYGWIN|MSYS)$" AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "event"))
    LIST(APPEND PTHREADPOOL_SRCS src/windows.c)
  ELSE()
    LIST(APPEND PTHREADPOOL_SRCS src/pthreads.c)
  ENDIF()
  IF(PTHREADPOOL_ENABLE_FASTPATH)
    LIST(APPEND PTHREADPOOL_SRCS src/fastpath.c)
  ENDIF()
ENDIF()

And after checking the implementation in that file, I found that all tasks are handled serially.

Here is a code snippet from shim.c,

void pthreadpool_parallelize_1d(
    struct pthreadpool* threadpool,
    pthreadpool_task_1d_t task,
    void* argument,
    size_t range,
    uint32_t flags)
{
    for (size_t i = 0; i < range; i++) {
        task(argument, i);
    }
}

As all tasks run in main thread, can I say that this thread pool does not support EMSCRIPTEN?

Maratyszcza commented 3 years ago

pthreadpool supports EMSCRIPTEN threads and even has optimizations for it, but only in Bazel build. CMake build always use single-thread fallback, mostly because Emscripten CMake toolchain doesn't define any variable to distinguish builds with threading enabled/disabled. If you're looking to enable multithreading in pthreadpool, I suggest to start with the Emscripten CMake toolchain.