Skip to content

Commit 137c1a8

Browse files
authored
Retry if pthread_create fails with EAGAIN (#824)
Signed-off-by: Rui Ueyama <ruiu@cs.stanford.edu>
1 parent ceacd22 commit 137c1a8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/tbb/rml_thread_monitor.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <pthread.h>
3232
#include <cstring>
3333
#include <cstdlib>
34+
#include <time.h>
3435
#else
3536
#error Unsupported platform
3637
#endif
@@ -191,8 +192,25 @@ inline thread_monitor::handle_type thread_monitor::launch( void* (*thread_routin
191192
check(pthread_attr_init( &s ), "pthread_attr_init has failed");
192193
if( stack_size>0 )
193194
check(pthread_attr_setstacksize( &s, stack_size ), "pthread_attr_setstack_size has failed" );
195+
196+
// pthread_create(2) can spuriously fail with EAGAIN. We retry
197+
// max_num_tries times with progressively longer wait times.
194198
pthread_t handle;
195-
check( pthread_create( &handle, &s, thread_routine, arg ), "pthread_create has failed" );
199+
const int max_num_tries = 20;
200+
int error = EAGAIN;
201+
202+
for (int i = 0; i < max_num_tries && error == EAGAIN; i++) {
203+
if (i != 0) {
204+
// Wait i milliseconds
205+
struct timespec ts = {0, i * 1000 * 1000};
206+
nanosleep(&ts, NULL);
207+
}
208+
error = pthread_create(&handle, &s, thread_routine, arg);
209+
}
210+
211+
if (error)
212+
handle_perror(error, "pthread_create has failed");
213+
196214
check( pthread_attr_destroy( &s ), "pthread_attr_destroy has failed" );
197215
return handle;
198216
}

0 commit comments

Comments
 (0)