Questions tagged [pthreads]

Pthreads (POSIX Threads) is a standardised C-based API for creating and manipulating threads. It is currently defined by POSIX.1-2008 (IEEE Std 1003.1, 2013 Edition / The Open Group Base Specifications Issue 7).

The API is mostly covered by the header documented at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html and the behaviour by http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09

See https://en.wikipedia.org/wiki/POSIX_Threads for more details and further reading. The POSIX.1-2008 Base Definitions is available online at http://pubs.opengroup.org/onlinepubs/9699919799/

POSIX Threads is also covered extensively in Programming with POSIX Threads by David Butenhof.

A port to MS-Windows (x86/x64) is available at: https://sourceware.org/pthreads-win32/

pthreads is also the name of an Object Oriented API that allows user-land multi-threading in PHP created by Joe Watkins

8869 questions
65
votes
13 answers

How to print pthread_t

Searched, but don't come across a satisfying answer. I know there's no a portable way to print a pthread_t. How do you do it in your app? Update: Actually I don't need pthread_t, but some small numeric id, identifying in debug message different…
dimba
  • 26,717
  • 34
  • 141
  • 196
65
votes
3 answers

sem_init on OS X

I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my Ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a…
Nippysaurus
  • 20,110
  • 21
  • 77
  • 129
64
votes
12 answers

How much overhead is there when creating a thread?

I just reviewed some really terrible code - code that sends messages on a serial port by creating a new thread to package and assemble the message in a new thread for every single message sent. Yes, for every message a pthread is created, bits are…
jdt141
  • 4,993
  • 6
  • 35
  • 36
61
votes
6 answers

Is Pthread library actually a user thread solution?

The title might not be clear enough because I don't know how to define my questions actually. I understand Pthread is a thread library meeting POSIX standard (about POSIX, see wikipedia: http://en.wikipedia.org/wiki/Posix). It is available in…
Mengfei Murphy
  • 1,049
  • 3
  • 11
  • 16
61
votes
7 answers

How do you query a pthread to see if it is still running?

In my destructor I want to destroy a thread cleanly. My goal is to wait for a thread to finish executing and THEN destroy the thread. The only thing I found about querying the state of a pthread is pthread_attr_setdetachstate but this only tells you…
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
60
votes
10 answers

When to use pthread_exit() and when to use pthread_join() in Linux?

I am new to pthreads, and I am trying to understand it. I saw some examples like the following. I could see that the main() is blocked by the API pthread_exit(), and I have seen examples where the main function is blocked by the API pthread_join().…
dexterous
  • 6,422
  • 12
  • 51
  • 99
58
votes
8 answers

Is there an invalid pthread_t id?

I would like to call pthread_join for a given thread id, but only if that thread has been started. The safe solution might be to add a variable to track which thread where started or not. However, I wonder if checking pthread_t variables is…
shodanex
  • 14,975
  • 11
  • 57
  • 91
57
votes
2 answers

Green-threads and thread in Python

As Wikipedia states: Green threads emulate multi-threaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread…
Rahul Gautam
  • 4,749
  • 2
  • 21
  • 30
56
votes
4 answers

How do you declare a recursive mutex with POSIX threads?

I am a bit confused on how to declare a recursive mutex using pthread. What I try to do is have only one thread at a time be able to run a piece of code(including functions) but after scepticism I figured out that the use of mutexes would not work…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
56
votes
5 answers

Why is a pthread mutex considered "slower" than a futex?

Why are POSIX mutexes considered heavier or slower than futexes? Where is the overhead coming from in the pthread mutex type? I've heard that pthread mutexes are based on futexes, and when uncontested, do not make any calls into the kernel. It…
Jason
  • 31,834
  • 7
  • 59
  • 78
52
votes
4 answers

Will main() catch exceptions thrown from threads?

I have a pretty large application that dynamically loads shared objects and executes code in the shared object. As a precaution, I put a try/catch around almost everything in main. I created a catch for 3 things: myException (an in house exception),…
steveo225
  • 11,394
  • 16
  • 62
  • 114
51
votes
3 answers

Difference between pthread and fork on gnu/Linux

What is the basic difference between a pthread and fork w.r.t. linux in terms of implementation differences and how the scheduling varies (does it vary ?) I ran strace on two similar programs , one using pthreads and another using fork, both in the…
srinathhs
  • 1,998
  • 4
  • 19
  • 33
50
votes
3 answers

How pthread_mutex_lock is implemented

I am just curious to know how functions related to synchronization between threads are implemented inside Unix. For example, what happens when I call pthread_mutex_lock? Are there any pointers in use? A reference to the source code would really…
avd
  • 13,993
  • 32
  • 78
  • 99
50
votes
10 answers

Building error using cmake: cannot find -lpthreads

I have c++ project that was smoothly running on a given machine, and now I am trying to compile it on another one with the same operating system (Xubuntu 14.04). I've installed all the dependencies and I'am using cmake to build the project, although…
Sapiens
  • 1,751
  • 2
  • 17
  • 19
50
votes
3 answers

Does pthread_cond_wait(&cond_t, &mutex); unlock and then lock the mutex?

I m using pthread_cond_wait(&cond_t, &mutex); in my program and I m wondering why this function needs as a second parameter a mutex variable. Does the pthread_cond_wait() unlock the mutex at the beginning (beginning of the execution…
MOHAMED
  • 41,599
  • 58
  • 163
  • 268