POSIX Threads: pthreads_cond_wait () and other system cards?

The POSIX standard defines several routines for synchronizing threads based on concepts, such as mutexes and conditional variables.

Now my question is: are these (e.g. pthreads_cond_init (), pthreads_mutex_init (), pthreads_mutex_lock () ... etc.) system calls or just library calls? I know that they are included through "pthread.h", but ultimately they lead to a system call and therefore are implemented in the kernel of the operating system?

+4
source share
4 answers

On Linux, mutex pthread creates a "futex" system call, but only if it blocks. This means that the fixation, which no other thread requires, is almost free.

Similarly, sending a condition signal is expensive when someone is waiting for it.

So, I think your answer is that pthread functions are library calls that sometimes lead to a system call.

+9
source

Whenever possible, the library avoids traps in the kernel for performance reasons. If you already have code that uses these calls, you can take a look at the result of your program using strace to better understand how often it actually makes system calls.

+2
source

I have never considered all these library calls, but as far as I understand, they are all related to kernel operations, since they should provide synchronization between the process and / or threads at the global level - I mean at the OS level.

The kernel needs to be supported for mutex , for example, a list of threads: threads that are currently sleeping, waiting for the locked mutex to be released . When a thread that is currently blocking / owning this mutex calls the kernel using pthread_mutex_release() , the kernel system call will look at this aforementioned list to get a thread with a higher priority waiting for mutex to be released, a flag for the new mutex owner in the kernel mutex structure, and then transfer the processor (aka "ontect switch") to the new owner's thread, so this process will return from the posix library call pthread_mutex_lock() .

I only see cooperation with the kernel when it is connected with IPC between processes (I do not speak between threads at the same process level). Therefore, I expect the library call to call the kernel, so.

+1
source

When you compile a program on Linux that uses pthreads, you need to add -lphtread to the compiler options. By doing this, you will give the linker a link to libpthreads. So on linux these are library calls.

-3
source

All Articles