I've created a small test program that exhibits the same behavior.  I've 
attached the .cpp and makefile.  I expected to see the workers in the 
thread pool reused, but I get new workers each time.  Why is this.  

Thanks in advance!

On Thursday, March 8, 2018 at 3:18:53 PM UTC-8, Scott Watson wrote:
>
> I am working on porting a general purpose media player to Javascript using 
> emscripten.  I am compiling to Javascript right now while we wait for 
> stable pthread support in WebAssembly.  
>
> My project is building and I can run it, but it crashes with a can't 
> allocate memory exception after a couple of minutes.  I realized that it is 
> related to the threads in my application.  I have approximately 12 
> long-lived threads and 3 threads that are relatively short-lived that are 
> stopped and started repeatedly.  I have set a pthread pool size of 18, but 
> I keep seeing messages indicating that another worker was allocated for the 
> spawn pool.  
>
> I initially thought that my threads weren't going away, and that was why I 
> was leaking memory.  I know that each worker gets a large chunk of memory. 
>  When I build with --threadprofiler I see that most of the threads "have 
> not yet started".  I'm not sure if that means the worker is finished and 
> waiting in the pool, or if it has never been started.  Either way, why 
> would a new worker be allocated for the pool if there are several that have 
> not yet started?  
>
> Thanks,
>
>
>

-- 
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.
#include <emscripten.h>

#include <thread>
#include <unistd.h>

#define SLEEP(x) usleep((useconds_t)(x*1000000))

std::thread worker1;
std::thread worker2;

bool stop=false;

void Worker1(void *parg) {
	emscripten_log(EM_LOG_CONSOLE, "Starting Worker1...");
	while (!stop) { SLEEP(0.10); }
	emscripten_log(EM_LOG_CONSOLE, "Worker1 complete!");
}

void Worker2(void *parg) {
	emscripten_log(EM_LOG_CONSOLE, "Starting Worker2...");
	while (!stop) { SLEEP(0.10); }
	emscripten_log(EM_LOG_CONSOLE, "Worker2 complete!");
}

void WorkerController(void *parg) {
	stop = true;

	emscripten_log(EM_LOG_CONSOLE, "Stopping worker threads...");
	if (worker1.joinable()) { worker1.join(); }
	if (worker2.joinable()) { worker2.join(); }

	stop = false;

	emscripten_log(EM_LOG_CONSOLE, "Worker threads stopped.");
	emscripten_log(EM_LOG_CONSOLE, "Starting worker threads...");

	worker1 = std::thread(Worker1, parg);
	worker2 = std::thread(Worker2, parg);

	emscripten_log(EM_LOG_CONSOLE, "Worker threads started.");
}

void Loop(void *parg) {
	std::thread(WorkerController, parg).detach();
}

int main(int argc, char *argv[]) {
    emscripten_set_main_loop_arg(Loop, NULL, 1, false);
}

Attachment: Makefile
Description: Binary data

Reply via email to