Pithikos/C-Thread-Pool

When a thread is in an endless loop, can it not exit and be destroyed?

Closed this issue · 1 comments

bsem_post_all(thpool_p->jobqueue.has_jobs);

(Just trying to help the maintainers close out some issues...)

This question is unclear. If the job (task) function currently running on a thread is in an actual endless loop, that's a bug that the user needs to fix rather than a problem with C-Thread-Pool. E.g., if the function contains, say, a "while(1)" or "for(;;)" loop, then the user must provide some mechanism to coerce a break out of that loop before thpool_destroy is called.

The loop you referenced just makes certain that any threads not exited in the one second allotted for the previous loop will be terminated before the thread-pool object and the associated job queue are destroyed. This second loop is guaranteed to terminate as long as the functions passed to all currently running jobs eventually return. Using two loops in this way is actually an elegant strategy, as applications using short-running jobs (<< 1 sec) will complete the thread-pool destruction very quickly, but it will also work correctly if many seconds are needed for the job functions to exit.