pthreads
How is GCC’s __thread implemented?
Recent GCC, e.g. GCC 5 do support C11 and its thread_local (if compiling with e.g. gcc -std=c11). As FUZxxl commented, you could use (instead of C11 thread_local) the __thread qualifier supported by older GCC versions. Read about Thread Local Storage. pthread_getspecific is indeed quite slow (it is in the POSIX library, so is not provided … Read more
C Programming: Debugging with pthreads
Valgrind is an excellent tool to find race conditions and pthreads API misuses. It keeps a model of program memory (and perhaps of shared resources) accesses and will detect missing locks even when the bug is benign (which of course means that it will completely unexpectedly become less benign at some later point). To use … Read more
Overhead of pthread mutexes?
All modern thread implementations can handle an uncontended mutex lock entirely in user space (with just a couple of machine instructions) – only when there is contention, the library has to call into the kernel. Another point to consider is that if an application doesn’t explicitly link to the pthread library (because it’s a single-threaded … Read more
Do mutexes guarantee ordering of acquisition? Unlocking thread takes it again while others are still waiting
Known problem. C++ mutexes are thin layer on top of OS-provided mutexes, and OS-provided mutexes are often not fair. They do not care for FIFO. The other side of the same coin is that threads are usually not pre-empted until they run out of their time slice. As a result, thread A in this scenario … Read more
What is the difference between Go’s multithreading and pthread or Java Threads?
Quoted from Day 3 Tutorial <- read this for more information. Goroutines are multiplexed as needed onto system threads. When a goroutine executes a blocking system call, no other goroutine is blocked. We will do the same for CPU-bound goroutines at some point, but for now, if you want user-level parallelism you must set $GOMAXPROCS. … Read more
Set CPU affinity when create a thread
I am sorry to be the “myth buster” here, but setting thread affinity has great importance, and it grows in importance over time as the systems we all use become more and more NUMA (Non-Uniform Memory Architecture) by nature. Even a trivial dual socket server these days has RAM connected separately to each socket, and … Read more
What is the _REENTRANT flag?
Defining _REENTRANT causes the compiler to use thread safe (i.e. re-entrant) versions of several functions in the C library. You can search your header files to see what happens when it’s defined.
Why does start_routine for pthread_create return void* and take void*
From the documentation for pthread_create: The thread is created executing start_routine with arg as its sole argument. If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status. Note that the thread in which main() was originally invoked differs from … Read more
Using C/Pthreads: do shared variables need to be volatile?
As long as you are using locks to control access to the variable, you do not need volatile on it. In fact, if you’re putting volatile on any variable you’re probably already wrong. https://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/