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
536
votes
16 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
  • 6,249
  • 7
  • 23
  • 19
388
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
  • 4,303
  • 4
  • 18
  • 9
261
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
  • 26,717
  • 34
  • 141
  • 196
207
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
  • 17,185
  • 12
  • 62
  • 78
186
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
  • 3,019
  • 5
  • 22
  • 25
180
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
178
votes
5 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
  • 37,181
  • 17
  • 72
  • 102
178
votes
4 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
  • 3,224
  • 3
  • 23
  • 17
168
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
  • 11,216
  • 17
  • 52
  • 54
156
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,819
  • 3
  • 19
  • 23
132
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
  • 42,832
  • 72
  • 186
  • 216
131
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,700
  • 9
  • 35
  • 42
126
votes
12 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,749
  • 4
  • 13
  • 22
119
votes
7 answers

Which is more efficient, basic mutex lock or atomic integer?

For something simple like a counter if multiple threads will be increasing the number. I read that mutex locks can decrease efficiency since the threads have to wait. So, to me, an atomic counter would be the most efficient, but I read that…
Matt
  • 1,199
  • 2
  • 8
  • 3
105
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,681
  • 9
  • 30
  • 49
1
2 3
99 100