pthread execution on linux

You are right in saying that the order of thread execution is not sequential. To some extent, that is the whole point of using threads, i.e. to run other tasks concurrently. The output you are seeing is as expected, and can possibly be different. Perhaps this will help: main thread1 thread2 | |–create——–+———–\ | | … Read more

How to sleep or pause a PThread in c on Linux

You can use a mutex, condition variable, and a shared flag variable to do this. Let’s assume these are defined globally: pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int play = 0; You could structure your playback code like this: for(;;) { /* Playback loop */ pthread_mutex_lock(&lock); while(!play) { /* We’re paused */ pthread_cond_wait(&cond, … 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

Pthreads vs. OpenMP

Pthreads and OpenMP represent two totally different multiprocessing paradigms. Pthreads is a very low-level API for working with threads. Thus, you have extremely fine-grained control over thread management (create/join/etc), mutexes, and so on. It’s fairly bare-bones. On the other hand, OpenMP is much higher level, is more portable and doesn’t limit you to using C. … Read more