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
33
votes
9 answers

Overhead of pthread mutexes?

I'm trying to make a C++ API (for Linux and Solaris) thread-safe, so that its functions can be called from different threads without breaking internal data structures. In my current approach I'm using pthread mutexes to protect all accesses to…
oliver
32
votes
8 answers

C Programming: Debugging with pthreads

One of the hardest things for me to initially adjust to was my first intense experience programming with pthreads in C. I was used to knowing exactly what the next line of code to be run would be and most of my debugging techniques centered around…
JoeCool
  • 4,392
  • 11
  • 50
  • 66
31
votes
3 answers

What is the difference between NPTL and POSIX threads?

What is the basic difference between NPTL and POSIX threads? How have these two evolved?
Whoami
  • 13,930
  • 19
  • 84
  • 140
31
votes
5 answers

valgrind memory leak errors when using pthread_create

I'm writing a program using the pthread library. When I run my program with the command valgrind --leak-check=full, I get the following errors description: ==11784== ==11784== **HEAP SUMMARY:** ==11784== in use at exit: 4,952 bytes in 18…
lim
  • 371
  • 1
  • 4
  • 4
31
votes
4 answers

what is the "attribute" of a pthread mutex?

The function pthread_mutex_init allows you to specify a pointer to an attribute. But I have yet to find a good explanation of what pthread attributes are. I have always just supplied NULL. Is there a use to this argument? The documentation, for…
vy32
  • 28,461
  • 37
  • 122
  • 246
31
votes
2 answers

How is GCC's __thread implemented?

How is __thread in gcc implemented? Is it simply a wrapper over pthread_getspecific and pthread_setspecific? With my program that uses the posix API for TLS, I'm kind of disappointed now seeing that 30% of my program runtime is spent on…
user3810155
31
votes
3 answers

What is PTHREAD_MUTEX_ADAPTIVE_NP

Where can I find documentation for "adaptive" pthread mutexes? The symbol PTHREAD_MUTEX_ADAPTIVE_NP is defined on my system, but the only documentation I can find online says nothing about what an adaptive mutex is, or when it's appropriate to…
laslowh
  • 8,482
  • 5
  • 34
  • 45
31
votes
1 answer

Php: when to use pthread

I don't know much about using threads but I looked into pthreads for php and it seems very interesting and easy, or easier than I thought... I searched for examples and looked through the documentation but I couldn't find any real-world examples of…
joschua011
  • 4,157
  • 4
  • 20
  • 25
30
votes
4 answers

Should I use Helgrind or DRD for thread error detection?

Looks like Valgrind has two tools that both do thread error detection: Helgrind and DRD. These tools are substantially similar. My primary question is: when should I use one instead of the other to check my multi-threaded code? More broadly, why…
Jeff Terrell Ph.D.
  • 2,563
  • 26
  • 39
30
votes
2 answers

When to use pthread_cancel and not pthread_kill?

When does one use pthread_cancel and not pthread_kill?
Sashi
  • 3,069
  • 6
  • 20
  • 18
30
votes
3 answers

Source code of PThread Library?

I am trying to find the source code of pthread library. (I guess its a supposed to be a part of Linux source code) But somehow can't find any good website that has it. I like this website: http://lxr.linux.no/#linux+v2.6.34.1/ where I usually find…
bits
  • 8,110
  • 8
  • 46
  • 55
30
votes
6 answers

gdb: Cannot find new threads: generic error

When I run GDB against a program which loads a .so which is linked to pthreads, GDB reports error "Cannot find new threads: generic error". Note that executable that I run is not linked with pthreads. Any clues? $ gdb --args lua…
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
30
votes
3 answers

When the main thread exits, do other threads also exit?

I have a problem about main threads and other threads in the same process. When the main function returns, do the other threads exit too? I am confused about this. Consider the following test code: void* test1(void *arg) { unsigned int i = 0; …
laifjei
  • 606
  • 2
  • 7
  • 16
29
votes
3 answers

Signal handling in pthreads

I have created a pthread, and installed a signal handler inside that, same way as we do in main( ) function. The thread's signal handler is a separate function. Surprisingly, it is not working, that is the thread's signal handler is not able to…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
29
votes
4 answers

Cancelling a thread using pthread_cancel : good practice or bad

I have a C++ program on Linux (CentOS 5.3) spawning multiple threads which are in an infinite loop to perform a job and sleep for certain minutes. Now I have to cancel the running threads in case a new configuration notification comes in and freshly…
Mandar
  • 693
  • 2
  • 9
  • 19