From f5d93caf15db4fc6ce50c553647e1f5509a97276 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Fri, 27 Dec 2019 15:47:43 +0100 Subject: [PATCH] xbps_array_foreach_cb_multi: improve previous (v2). As suggested by @CasperVector reuse the 'i' var to wait for threads that were created successfully. --- lib/plist.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/plist.c b/lib/plist.c index afa2a974..8ed5e9f6 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -102,7 +102,7 @@ xbps_array_foreach_cb_multi(struct xbps_handle *xhp, { struct thread_data *thd; unsigned int arraycount, slicecount; - int rv = 0, error = 0 , maxthreads; + int rv = 0, error = 0, i, maxthreads; unsigned int reserved; pthread_spinlock_t reserved_lock; @@ -138,7 +138,7 @@ xbps_array_foreach_cb_multi(struct xbps_handle *xhp, reserved = slicecount * maxthreads; - for (int i = 0; i < maxthreads; i++) { + for (i = 0; i < maxthreads; i++) { thd[i].array = array; thd[i].dict = dict; thd[i].xhp = xhp; @@ -156,9 +156,11 @@ xbps_array_foreach_cb_multi(struct xbps_handle *xhp, } } - /* wait for all threads */ - for (int i = 0; i < maxthreads; i++) - rv = pthread_join(thd[i].thread, NULL); + /* wait for all threads that were created successfully */ + for (int c = 0; c < i; c++) { + if ((rv = pthread_join(thd[c].thread, NULL)) != 0) + error = rv; + } out: free(thd);