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

8207 questions
441
votes
15 answers

Undefined reference to pthread_create in Linux

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/ #include #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; …
Ralph
  • 5,169
  • 7
  • 21
  • 19
342
votes
7 answers

What's the difference between deadlock and livelock?

Can somebody please explain with examples (of code) what is the difference between deadlock and livelock?
macindows
  • 3,813
  • 4
  • 15
  • 9
223
votes
4 answers

cmake and libpthread

I'm running RHEL 5.1 and use gcc. How I tell cmake to add -pthread to compilation and linking?
dimba
  • 24,103
  • 28
  • 127
  • 188
194
votes
10 answers

Why do pthreads’ condition variable functions require a mutex?

I’m reading up on pthread.h; the condition variable related functions (like pthread_cond_wait(3)) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed…
ELLIOTTCABLE
  • 14,649
  • 11
  • 49
  • 70
164
votes
4 answers

C++11 std::threads vs posix threads

Why should I prefer one or another in practice? What are technical differences except that std::thread is a class?
Shamdor
  • 2,699
  • 3
  • 20
  • 24
163
votes
5 answers

Still Reachable Leak detected by Valgrind

All the functions mentioned in this block are library functions. How can I rectify this memory leak? It is listed under the "Still reachable" category. (There are 4 more, which are very similar, but of varying sizes) 630 bytes in 1 blocks are…
user191776
162
votes
4 answers

Why does pthread_cond_wait have spurious wakeups?

To quote the man page: When using condition variables there is always a Boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_timedwait()…
Jonathan M Davis
  • 34,779
  • 15
  • 69
  • 99
152
votes
2 answers

Significance of -pthread flag when compiling

In various multi threaded C and C++ projects I've seen the -pthread flag applied to both the compiling and linking stage while others don't use it at all and just pass -lpthread to the linking stage. Is there any danger not compiling and linking…
leeeroy
  • 10,518
  • 17
  • 48
  • 52
152
votes
3 answers

mingw-w64 threads: posix vs win32

I'm installing mingw-w64 on Windows and there are two options: win32 threads and posix threads. I know what is the difference between win32 threads and pthreads but I don't understand what is the difference between these two options. I doubt that if…
Simon
  • 2,794
  • 3
  • 20
  • 17
122
votes
3 answers

Difference between -pthread and -lpthread while compiling

What is the difference between gcc -pthread and gcc -lpthread which is used while compiling multithreaded programs?
Vishnuraj V
  • 2,398
  • 3
  • 15
  • 23
109
votes
4 answers

What is the Re-entrant lock and concept in general?

I always get confused. Would someone explain what Reentrant means in different contexts? And why would you want to use reentrant vs. non-reentrant? Say pthread (posix) locking primitives, are they re-entrant or not? What pitfalls should be avoided…
vehomzzz
  • 37,854
  • 69
  • 173
  • 207
101
votes
9 answers

Multiple arguments to function called by pthread_create()?

I need to pass multiple arguments to a function that I would like to call on a separate thread. I've read that the typical way to do this is to define a struct, pass the function a pointer to that, and dereference it for the arguments. However, I am…
Michael
  • 4,300
  • 9
  • 32
  • 41
98
votes
11 answers

How to get thread id of a pthread in linux c program?

In a Linux C program, how do I print the thread id of a thread created by the pthread library? For example like how we can get pid of a process by getpid().
Ravi Chandra
  • 1,401
  • 3
  • 12
  • 19
95
votes
4 answers

PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)

Is there any difference between pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; Or pthread_mutex_t lock; pthread_mutex_init ( &lock, NULL); Am I safe enough if I use only the first method ? NOTE: My question mostly refers to very small programs…
Kalec
  • 2,380
  • 6
  • 26
  • 42
88
votes
9 answers

pthread function from a class

Let's say I have a class such as class c { // ... void *print(void *){ cout << "Hello"; } } And then I have a vector of c vector classes; pthread_t t1; classes.push_back(c()); classes.push_back(c()); Now, I want to create a thread on…
Angel.King.47
  • 7,514
  • 14
  • 58
  • 84
1
2 3
99 100