is glibc c11 thread implementation a wrapper on top of pthread? - multithreading

to build a c11 thread program with glibc I still need link with -lpthread,why? glibc 2.28 claims c11 thread support but why I need pthread still?
musl can build c11 thread without pthread though.

Yes, glibc's C11 threads implementation is using pthreads library underneath and that's why it needs linking with pthread library. In glibc, pthreads is (was always) a separate library - thus the need to link it.
Whereas in musl library, the thread implementation is a part of the main C library itself and thus there's no need to link any thread library whether you use pthreads or C11 threads with musl. Also, see https://www.openwall.com/lists/musl/2012/07/25/3.

Related

is g_timeout_add thread-safe (Linux, GTK3, X11)?

Context: on Linux/Debian/Sid x86-64 for my bismon (GPLv3+) software (described here)(GTK 3.24, Glib 2.62, Xorg server, function register_gui_postponed_BM, file gui_GTKBM.c)
Question:
Can g_timeout_add be safely called from some other thread than the main thread? I can easily ensure that call would be serialized (using a pthread mutex) but I cannot guarantee it would be called from the main thread. The threads are Pthreads, not Glib threads.
I found both this and that, and they make me think it is indeed safe.... But I might have understood wrongly.
Yes. It boils down to a call to g_source_attach(), which locks the GMainContext it adds the timeout source to.

C++11: Is std::thread on linux depending on pthread library?

I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.
So I wish to know on linux system, how gcc/clang implements std::thread, is it calling some linux native functions/kernel apis or something?
Also, how is std::thread_local implemented, related with __thread?
I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.
This information is inaccurate.
how gcc/clang implements std::thread
They call a platform-specific thread creation function. On Linux it is pthread_create. You can call this function directly.
When a thread throws an exception and it is not caught std::terminate is called.
Note that your application must be compiled and linked with -pthread flag (using -lpthread is unnecessary and insufficient with both C and C++).
I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.
There's a statement in the neighborhood of this that is true, but this statement as written is not true.
There are two facts here.
If YOU call pthreads functions yourself, it is indeed just a C library, and you had better make sure you do everything correctly in regards to exception safety. If you pass function pointers to pthread_create_... and those functions will throw exceptions... your program can have big problems. That should be obvious, it will be true whenever you talk to a C library from C++.
That does not mean it is impossible to use such a library with a C++ program!
pthread does not actually need to know about any of your objects, or any of their ctors or dtors, or any of that, in order to make your program multithreaded. All it needs to spawn a thread, is a function pointer, and that function pointer will have a completely C-compatible signature.
When the C++ compiler calls pthreads functions in order to implement std::thread, the compiler is going to emit code that talks to pthread correctly. If it uses pthread in an illegal way to implement your C++ program, it's a bug in the compiler or standard library.
Use ldd myExecutable on compiler output to find out.
Both libstdc++ and libc++ apparently use pthreads, but they are not required to do that. Evidence of it can be found in native_handle methods documentation here and here. The documents say:
Accesses the native handle of *this.
The meaning and the type of the result of this function is implementation-defined. On a POSIX system, this may be a value of type pthread_cond_t*. On a Windows system, this may be a PCONDITION_VARIABLE.
and
Returns the implementation defined underlying thread handle.

Kernel Level Thread Library

I have to implement kernel level thread but while searching on the net I found that there are three ways to create kernel level thread in linux:
NPTL
kthread
linuxThreads
It was written somewhere that linuxThreads are now abandoned. But I am unable to find current support of NPTL & kthread. Also I am unable to find any source that can simply explain me how to use their functionality.
Which is the currently supported and good library to use kernel level thread?
Also pls share any resource for installing these library and also using them?
You are confusing two very different definitions of "kernel thread".
LinuxThreads and NPTL are implementations of POSIX pthreads for user-space processes. They use a 1-to-1 mapping of kernel scheduling entities to user-space threads. They are sometimes described as kernel threads implementations only because they create threads that are scheduled by the kernel.
LinuxThreads is unsupported and entirely obsolete. NPTL is now part of glibc, so you already have it. There's nothing special to install. You use these the same way you use any POSIX threading library, with calls to functions like pthread_create.
Actual kernel threads run kernel code. None of those libraries are relevant since they're all user-space libraries. Have a look at functions like kthread_run. There's no magic, no secret. Write kernel code the way similar kernel code is written. (Knowledge and experience in writing kernel code is needed. It's, unfortunately, not simple.)
I assume that; if you really wanted to create a kernel thread, you would already know about these things.
I think, you want to create multi-threaded applications and trying to find info about user-level multi-threading functions.
And yes, these threads you created will be managed by the kernel itself. This is what you are looking for :: POSIX Threads

Can GDB display a list of pthread mutexes held by each thread?

I have GDB attached to a deadlocked application written with pthreads. There are ~10 threads that are all blocked, and I'd like to know which locks are held by which threads. This is possible in WinDbg using SOS.dll; is this possible in GDB?
On at least one flavor of Linux, the C++11 std::mutex has a member called __owner which contains the thread id of the thread that currently has the mutex locked. Using "info threads" in gdb shows the thread numbers used by gdb along with the thread ids (see the "LWP" number), allowing you to switch to that thread ("thread N") and then examine the call stack ("backtrace").
It's not GDB you should be asking about, but rather the specific pthread library and OS you are using.
The pthread library implements mutexes in cooperation with the kernel via some set of system calls. If its implementation of mutexes embeds something to tie the last thread holding the mutex into the mutex data structure, then you can use GDB to get at that information.
It is possible your kernel tracks that information. Under Mac OS X, for example, the collection of GDB scripts bundled with the kernel debugging kit, kgmacros, includes a command showallmtx that will do exactly what you want. The catch: to use it, you have to be debugging the machine's kernel at the time, which means you need to be doing the debugging using a different machine.
Of course, you might have a /dev/kmem device file, which would let you poke around in the kernel's memory and access the necessary data structure, provided you can locate it.
But this all really depends on your system - your pthread library and OS kernel - not on GDB.
You could also try creating a mutex of type PTHREAD_MUTEX_ERRORCHECK; this will cause pthread_mutex_lock() to return EDEADLK instead of deadlocking. You can then break when that occurs and root around in your non-deadlocked process.
GDB could be able to display this information, but they didn't implement this functionality: it requires a cooperation between the debugger and the thread library, though the libthread_db library.
DBX under Solaris -- at least -- (both from Sun, it helps!) implements correctly this feature (look for the Locks part)

Difference between sem_init and sema_init

What is the difference between sema_init and sem_init ? Are there any specific usage scenarios and other dependencies for the respective APIs ?
Since you tagged this with "linux" I'll ignore other Unixes.
sema_init is the Linux kernel's counting semaphore implementation initialization function.
sem_init is the initializer from the Posix thread library (and is therefore used by userspace code).
sema_init is from the Solaris thread library.
sem_init is from the Posix pthread library.
See Threads: Basic Theory and Libraries

Resources