views:

106

answers:

4

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?

A: 

When you compile a program on Linux that uses pthreads, you have to add -lphtread to the compiler options. by doing this, you tell the linker to link libpthreads. So, on linux, they are calls to a library.

andremo
By the same logic, `open()`, `read()`, `write()`, `close()` (and many others) are also just "calls to a library" (in this case the standard library). Actually, they're wrapper functions around syscall traps, and the POSIX threads library can do the same.
Artelius
if you go down far enough, you will always find a place where you hit the syscall. from the calling program point of view, it calls a library. the fact the library does a syscall on it's turn, doesn't change how the calling program calls the library.
andremo
But if someone is specifically asking about system calls, you have to assume that what they really want to know is whether a system call is the result of the library call or not... And no, not all library calls result in system calls (for example, much of the functionality in math.h is not implemented in terms of system calls).
dicroce
+1  A: 

I never looked into all those library call , but as far as I understand they all involve kernel operations as they are supposed to provide synchronisations between process and/or threads at global level - I mean at the OS level.

The kernel need to maintain for a mutex, for instance, a thread list: threads that are currently sleeping, waiting that a locked mutex get released. When the thread that currently lock/owns that mutex invokes the kernel with pthread_mutex_release(), the kernel system call will browse that aforementioned list to get the higher priority thread that is waiting for the mutex release, flag the new mutex owner into the mutex kernel structure, and then will give away the cpu (aka "ontect switch") to the newly owner thread, thus this process will return from the posix library call pthread_mutex_lock().

I only see a cooperation with the kernel when it involves IPC between processes (I am not talking between threads at a single process level). Therefore I expect those library call to invoke the kernel, so.

yves Baumes
+7  A: 

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.

Zan Lynx
Great answer... Totally didn't know that futex() was only for contended locks...
dicroce
+1  A: 

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.

Tim Kryger