bshoshany/thread-pool

[REQ] ability to set threads to 0, and have everything execute serially

wkjarosz opened this issue · 3 comments

This looks like a great library, and I'm considering using it. However, I'd like my application to optionally work without any additional threads being created (for instance, if I want to avoid using pthreads when compiling my code with emscripten for the web).

I would love to use your library, but currently I'd have to manually write separate code for the single-threaded version. Instead, I'd like to submit tasks, loops, etc, but if the number of threads in the threadpool is set to 0 (or some compile-time macro is set) no worker threads should ever be created, and everything should just execute in serial.

As an example, the openexr library allows setting the number of worker threads to 0 (or compiling with threading disabled), and in that case everything runs in a main thread, even though the code is written in terms of a threadpool and tasks.

Is this something you would consider?

Hi @wkjarosz and thanks for making this suggestion! I think this can be implemented, but I need a bit more information, since unfortunately I don't have much experience with Emscripten. When you say you want to avoid using pthreads, do you mean that Emscripten does not support the C++11 Standard Library <thread> header (or related headers such as <future>, <mutex>, etc.) at all? If so, then I imagine just setting the number of threads to 0 would not be enough, since that's a runtime value; it would have to be a macro that completely turns off anything in the thread support library at compilation time and just runs everything in kind of a single-threaded "simulation" of multi-threading.

Including <thread>, <future>, <mutex> should be fine for my use case -- this would all work fine with emscripten; but it would nonetheless be nice to guarantee that no threads are ever launched and all work happens in the calling thread (e.g. when calling .get() on the future; much like when using std::async with policy std::launch::deferred).

Background: pthreads do work with emscripten, but it can also make the resulting web app much more heavy weight, particularly if the amount of memory usage is not known in advance; so i'd like the option of building the web app without threading enabled.

Thanks for the explanation! I'll look into it. However, it might take a while, since I'm currently working on other projects. Meanwhile I'm closing this issue, but rest assured that this item is on my TODO list for when I resume working on this project (in 1-2 months).