Pthread_create does not work with EAGAIN

Consider this piece of code here, where I am trying to create a bunch of threads that end up processing a task that mimics a race condition.

const int thread_count = 128;
pthread_t threads[thread_count];

for (int n = 0; n != thread_count; ++n)
{
    ret = pthread_create(&threads[n], 0, test_thread_fun, &test_thread_args);
    if( ret != 0 )
    {
        fprintf( stdout, "Fail %d %d", ret, errno );
        exit(0);
     }
 }

Things usually work fine, except that sometimes pthread_create fails with the errno EAGAIN resource, which is temporarily unavailable, I tried to get to sleep and try to create, but without real effect.

failure is sporadic, and on some boxes there are no failures, and on some it happens very often.

any idea what might be wrong here?

Change - 1

update max-threads

cat /proc/sys/kernel/threads-max
256467

Edit 2

I think the inputs here made me think, I will probably do lower and post any results that are worth sharing.

  • , , thread_function .
  • ( )
  • script, , / , , , , .
  • , , .
+4
3

. : cat /proc/sys/kernel/threads-max

+1

ulimit -a . , , Linux .

0

If your program guarantees that it never creates more threads than it limits the system limit (adding threads before creating new ones), you will probably encounter this kernel error:

When using certain types of container technology, the race window seems much larger and the error is much easier to run. (This may depend on the types of groups used.)

0
source

Source: https://habr.com/ru/post/1688605/


All Articles