Does the link to "-lpthread" affect application behavior? (Linux, Glibc)

I have a question: if we have an application that does not use threads, we can link it in two ways:

1) Link, as usual, without -lpthread and -ldl

2) Add two libraries to the link: libpthread and libdl.

eg.

 $ cat ac int main(){printf("Hehe");} $ gcc ac -w -oa $ gcc ac -w -o a1 -ldl -lpthread 

By default, both libraries are dynamically linked:

 $ ldd a linux-gate.so.1 libc.so.6 /lib/ld-linux.so.2 $ ldd a1 linux-gate.so.1 libdl.so.2 libpthread.so.0 libc.so.6 /lib/ld-linux.so.2 

How much difference will there be between version a and version a1 ? What will work differently inside the application and int glibc? Will binding pthreads change anything internally from unsafe to thread safe?

eg.

 $ strace ./a 2>&1 |wc -l 73 $ strace ./a1 2>&1 |wc -l 103 

In trace a1, two additional libraries are loaded, a few more mprotect called, and a section is added:

  set_tid_address; set_robust_list; rt_sigaction x 2; rt_sigprocmask; getrlimit; uname 
+11
linux pthreads glibc
Jun 07 2018-11-11T00:
source share
1 answer

glibc itself contains stub code for many pthread functions. These glibc pthread functions do nothing. However, when a program is associated with libpthread, these stubs are replaced with real pthread blocking functions.

This is intended for use in libraries that should be thread safe, but do not use the streams themselves. These libraries can use pthread locks, but these locks will not actually be executed until the program or library that references libpthread is loaded.

+14
Jun 07 2018-11-11T00:
source share



All Articles