Skip to content

Commit

Permalink
Retry if pthread_create fails with EAGAIN (#824)
Browse files Browse the repository at this point in the history
Signed-off-by: Rui Ueyama <ruiu@cs.stanford.edu>
  • Loading branch information
rui314 authored Oct 26, 2022
1 parent ceacd22 commit 137c1a8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/tbb/rml_thread_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <pthread.h>
#include <cstring>
#include <cstdlib>
#include <time.h>
#else
#error Unsupported platform
#endif
Expand Down Expand Up @@ -191,8 +192,25 @@ inline thread_monitor::handle_type thread_monitor::launch( void* (*thread_routin
check(pthread_attr_init( &s ), "pthread_attr_init has failed");
if( stack_size>0 )
check(pthread_attr_setstacksize( &s, stack_size ), "pthread_attr_setstack_size has failed" );

// pthread_create(2) can spuriously fail with EAGAIN. We retry
// max_num_tries times with progressively longer wait times.
pthread_t handle;
check( pthread_create( &handle, &s, thread_routine, arg ), "pthread_create has failed" );
const int max_num_tries = 20;
int error = EAGAIN;

for (int i = 0; i < max_num_tries && error == EAGAIN; i++) {
if (i != 0) {
// Wait i milliseconds
struct timespec ts = {0, i * 1000 * 1000};
nanosleep(&ts, NULL);
}
error = pthread_create(&handle, &s, thread_routine, arg);
}

if (error)
handle_perror(error, "pthread_create has failed");

check( pthread_attr_destroy( &s ), "pthread_attr_destroy has failed" );
return handle;
}
Expand Down

0 comments on commit 137c1a8

Please sign in to comment.