Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX Threads: are pthreads_cond_wait() and others systemcalls?

The POSIX standard defines several routines for thread synchronization, based on concepts like mutexes and conditional variables.

my question is now: are these (like e.g. pthreads_cond_init(), pthreads_mutex_init(), pthreads_mutex_lock()... and so on) system calls or just library calls? i know they are included via "pthread.h", but do they finally result in a system call and therefore are implemented in the kernel of the operating system?

like image 453
Michael Zettler Avatar asked Jan 24 '23 02:01

Michael Zettler


2 Answers

On Linux a pthread mutex makes a "futex" system call, but only if the lock is contended. That means that taking a lock no other thread wants is almost free.

In a similar way, sending a condition signal is only expensive when there is someone waiting for it.

So I believe that your answer is that pthread functions are library calls that sometimes result in a system call.

like image 110
Zan Lynx Avatar answered Feb 03 '23 07:02

Zan Lynx


Whenever possible, the library avoids trapping into the kernel for performance reasons. If you already have some code that uses these calls you may want to take a look at the output from running your program with strace to better understand how often it is actually making system calls.

like image 22
Tim Kryger Avatar answered Feb 03 '23 08:02

Tim Kryger