How can I wait for any/all pthreads to complete?

Do you want your main thread to do anything in particular after all the threads have completed? If not, you can have your main thread simply call pthread_exit() instead of returning (or calling exit()). If main() returns it implicitly calls (or behaves as if it called) exit(), which will terminate the process. However, if main() … Read more

Does guarding a variable with a pthread mutex guarantee it’s also not cached?

pthread locks implement memory barriers that will ensure that cache effects are made visible to other threads. You don’t need volatile to properly deal with the shared variable i if the accesses to the shared variable are protected by pthread mutexes. from http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11: The following functions synchronize memory with respect to other threads: fork() pthread_barrier_wait() … Read more

How to prevent writer starvation in a read write lock in pthreads

This does indeed depend on the implementation – so since you have asked about Linux specifically, my comments are refer to the current NPTL implementation of pthreads, which is used in modern glibc. There are two related, but separate, issues here. Firstly, there is this situation: There are read locks currently held, and writers waiting. … Read more

What does “Performing Test CMAKE_HAVE_LIBC_PTHREAD” failed actually mean?

The lines — Looking for pthread.h — Looking for pthread.h – found — Performing Test CMAKE_HAVE_LIBC_PTHREAD — Performing Test CMAKE_HAVE_LIBC_PTHREAD – Failed — Looking for pthread_create in pthreads — Looking for pthread_create in pthreads – not found — Looking for pthread_create in pthread — Looking for pthread_create in pthread – found are output of a … Read more

Mutex lock threads

What you need to do is to call pthread_mutex_lock to secure a mutex, like this: pthread_mutex_lock(&mutex); Once you do this, any other calls to pthread_mutex_lock(mutex) will not return until you call pthread_mutex_unlock in this thread. So if you try to call pthread_create, you will be able to create a new thread, and that thread will … Read more

when g++ static link pthread, cause Segmentation fault, why?

First, the solution. This here will work: Update: Since Ubuntu 18.04, you need to link also against librt (add -lrt): g++ -o one one.cpp -Wall -std=c++11 -O3 -static -lrt -pthread \ -Wl,–whole-archive -lpthread -Wl,–no-whole-archive (continue with original answer) g++ -o one one.cpp -Wall -std=c++11 -O3 -static -pthread \ -Wl,–whole-archive -lpthread -Wl,–no-whole-archive When you use -pthread, … Read more

Do I need -D_REENTRANT with -pthreads?

For me the best answer was the comment from pts if only he bothered to submit it as answer: You investigated properly and answered your own question. Use g++ -pthread, it is equivalent to g++ -lpthread -D_REENTRANT. Using g++ -D_REENTRANT would be different, it may not set all the linker flags. – pts May 18 … Read more

How to continue one thread at a time when debugging a multithreaded program in GDB?

By default, GDB stops all threads when any breakpoint is hit, and resumes all threads when you issue any command (such as continue, next, step, finish, etc.) which requires that the inferior process (the one you are debugging) start to execute. However, you can tell GDB not to do that: (gdb) help set scheduler-locking Set … Read more