pthread_setattr_default_np race condition with pthread_create.
When creating a new thread with pthread_create and a NULL pthread_attr_t* argument, the pthread_attr_t* argument must come from the last call to pthread_setattr_default_np preceding the call to pthread_create.
It seems that there is a race condition in glibc implementation such that pthread_attr_t* argument comes from a pthread_setattr_default_np call that succeeds a pthread_create call.
Example:
[max@supernova:~/src/test] $ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.27-3ubuntu1) stable release version 2.27.
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 7.3.0.
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.
[max@supernova:~/src/test] $ g++ --version
g++ (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
pthread_ setattr_ default_ np race condition with pthread_create.
When creating a new thread with pthread_create and a NULL pthread_attr_t* argument, the pthread_attr_t* argument must come from the last call to pthread_ setattr_ default_ np preceding the call to pthread_create.
It seems that there is a race condition in glibc implementation such that pthread_attr_t* argument comes from a pthread_ setattr_ default_ np call that succeeds a pthread_create call.
Example:
[max@supernova: ~/src/test] $ /lib/x86_ 64-linux- gnu/libc. so.6 /bugs.launchpad .net/ubuntu/ +source/ glibc/+ bugs>.
GNU C Library (Ubuntu GLIBC 2.27-3ubuntu1) stable release version 2.27.
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 7.3.0.
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<https:/
[max@supernova: ~/src/test] $ g++ --version 18.04.1) 8.3.0
g++ (Ubuntu 8.3.0-6ubuntu1~
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[max@supernova: ~/src/test] $ cat test.cc
#include <cstdio>
#include <thread>
#include <system_error>
#include <pthread.h>
void set_default_ thread_ stack_size( size_t size) { attr_init( &attr)) error(err, std::system_ category( ), "pthread_ attr_init" ); attr_setstacksi ze(&attr, size)) error(err, std::system_ category( ), "pthread_ attr_setstacksi ze"); setattr_ default_ np(&attr) ) error(err, std::system_ category( ), "pthread_ setattr_ default_ np"); attr_destroy( &attr)) error(err, std::system_ category( ), "pthread_ attr_destroy" );
pthread_attr_t attr;
if(int err = ::pthread_
throw std::system_
if(int err = ::pthread_
throw std::system_
if(int err = ::pthread_
throw std::system_
if(int err = ::pthread_
throw std::system_
}
void another_thread() { attr_init( &attr)) error(err, std::system_ category( ), "pthread_ attr_init" ); attr_getstacksi ze(&attr, &stack_size)) error(err, std::system_ category( ), "pthread_ attr_getstacksi ze"); attr_destroy( &attr)) error(err, std::system_ category( ), "pthread_ attr_destroy" ); :printf( "thread stack size is %zu\n", stack_size);
pthread_attr_t attr;
if(int err = ::pthread_
throw std::system_
size_t stack_size;
if(int err = ::pthread_
throw std::system_
if(int err = ::pthread_
throw std::system_
std:
}
int main() { thread( ); default_ thread_ stack_size( 1024 * 1024);
another_
std::thread t0(another_thread);
set_
std::thread t1(another_thread);
t0.join();
t1.join();
}
[max@supernova: ~/src/test] $ g++ -std=gnu++14 -W{all,extra,error} -O3 -march=native -o test test.cc -pthread
[max@supernova: ~/src/test] $ ./test
thread stack size is 8720384
thread stack size is 1048576
thread stack size is 1048576
The above output is incorrect. The expected output is:
thread stack size is 8720384
thread stack size is 8720384
thread stack size is 1048576
If `t0.join();` line is moved to right after `std::thread t0(another_ thread) ;` line then the code produces the correct output.
It looks that setting the default pthread_attr_t is not atomic with respect to pthread_create calls.