@@ -678,9 +678,11 @@ PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
678678 /* Make changes by other threads visible to this thread. */
679679 pthreadpool_fence_acquire ();
680680
681- /* Make sure the threadpool is idle. */
682- assert (pthreadpool_load_consume_int32_t (& threadpool -> num_active_threads ) ==
683- 0 );
681+ /* Make sure the threadpool is idle or done. */
682+ const int32_t num_active_threads =
683+ pthreadpool_load_consume_int32_t (& threadpool -> num_active_threads );
684+ assert (num_active_threads == 0 ||
685+ num_active_threads == PTHREADPOOL_NUM_ACTIVE_THREADS_DONE );
684686
685687 /* Setup global arguments */
686688 pthreadpool_store_relaxed_void_p (& threadpool -> thread_function ,
@@ -754,7 +756,9 @@ PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
754756
755757static void pthreadpool_release_all_threads (struct pthreadpool * threadpool ) {
756758 if (threadpool != NULL ) {
757- assert (threadpool -> num_active_threads == 0 );
759+ assert (threadpool -> num_active_threads == 0 ||
760+ threadpool -> num_active_threads ==
761+ PTHREADPOOL_NUM_ACTIVE_THREADS_DONE );
758762
759763 // Set the state to "done".
760764 pthreadpool_store_sequentially_consistent_int32_t (
@@ -765,13 +769,6 @@ static void pthreadpool_release_all_threads(struct pthreadpool* threadpool) {
765769
766770 /* Wake up any thread waiting on a change of state. */
767771 signal_num_active_threads (threadpool , 0 );
768-
769- // Wait for any pending jobs to complete.
770- wait_on_num_recruited_threads (threadpool , 0 );
771-
772- // Set the state back to "idle".
773- pthreadpool_store_sequentially_consistent_int32_t (
774- & threadpool -> num_active_threads , 0 );
775772 }
776773}
777774
@@ -786,6 +783,9 @@ void pthreadpool_destroy(struct pthreadpool* threadpool) {
786783 /* Tell all threads to stop. */
787784 pthreadpool_release_all_threads (threadpool );
788785
786+ // Wait for any recruited threads to leave.
787+ wait_on_num_recruited_threads (threadpool , 0 );
788+
789789 if (!threadpool -> executor .num_threads ) {
790790 /* Wait until all threads return */
791791 for (size_t thread = 1 ; thread < threadpool -> max_num_threads ; thread ++ ) {
0 commit comments