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
50
votes
2 answers

Why is a while loop needed around pthread wait conditions?

I'm learning pthread and wait conditions. As far as I can tell a typical waiting thread is like this: pthread_mutex_lock(&m); while(!condition) pthread_cond_wait(&cond, &m); // Thread stuff here pthread_mutex_unlock(&m); What I can't…
Emiliano
  • 22,232
  • 11
  • 45
  • 59
49
votes
8 answers

Pthread mutex assertion error

I'm encountering the following error at unpredictable times in a linux-based (arm) communications application: pthread_mutex_lock.c:82: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed. Google turns up a lot of references to that…
Dave Causey
  • 13,098
  • 7
  • 30
  • 26
48
votes
5 answers

How do I get a thread ID from an arbitrary pthread_t?

I have a pthread_t, and I'd like to change its CPU affinity. The problem is that I'm using glibc 2.3.2, which doesn't have pthread_setaffinity_np(). That's OK, though, because pthread_setaffinity_np() is itself a wrapper of sched_setaffinity(),…
Skrud
  • 11,604
  • 5
  • 24
  • 22
48
votes
5 answers

Program received signal SIGPIPE, Broken pipe

I write a client program based on posix sockets. The program creates multiple threads and is going to lock the server. But during debug in gdb time the program gives an info (error) (gdb) n Program received signal SIGPIPE, Broken pipe. [Switching…
user1886376
47
votes
6 answers

pthread_exit vs. return

I have a joinable pthread runner function defined as below: void *sumOfProducts(void *param) { ... pthread_exit(0); } This thread is supposed to join the main thread. Whenever I ran my program through Valgrind I would get the following leaks: LEAK…
user191776
47
votes
5 answers

Kill Thread in Pthread Library

I use pthread_create(&thread1, &attrs, //... , //...); and need if some condition occured need to kill this thread how to kill this ?
Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
46
votes
5 answers

pthread_create and passing an integer as the last argument

I have the following functions : void *foo(void *i) { int a = (int) i; } int main() { pthread_t thread; int i; pthread_create(&thread, 0, foo, (void *) i); } At compilation, there are some errors about casting ((void *) i and int a…
Gradient
  • 2,253
  • 6
  • 25
  • 36
45
votes
6 answers

Parallelization: pthreads or OpenMP?

Most people in scientific computing use OpenMP as a quasi-standard when it comes to shared memory parallelization. Is there any reason (other than readability) to use OpenMP over pthreads? The latter seems more basic and I suspect it could be…
hanno
  • 6,401
  • 8
  • 48
  • 80
45
votes
3 answers

Advantages of using condition variables over mutex

I was wondering what is the performance benefit of using condition variables over mutex locks in pthreads. What I found is : "Without condition variables, the programmer would need to have threads continually polling (possibly in a critical…
Abhi
  • 1,167
  • 2
  • 14
  • 21
44
votes
6 answers

How to set the stacksize with C++11 std::thread

I've been trying to familiarize myself with the std::thread library in C++11, and have arrived at a stumbling block. Initially I come from a posix threads background, and was wondering how does one setup the stack size of the std::thread prior to…
Zamfir Kerlukson
  • 1,046
  • 1
  • 10
  • 20
43
votes
3 answers

pthread_join() and pthread_exit()

I have a question about C concurrency programming. In the pthread library, the prototype of pthread_join is int pthread_join(pthread_t tid, void **ret); and the prototype of pthread_exit is: void pthread_exit(void *ret); So I am confused that, why…
Allan Jiang
  • 11,063
  • 27
  • 104
  • 165
42
votes
4 answers

When is pthread_spin_lock the right thing to use (over e.g. a pthread mutex)?

Given that pthread_spin_lock is available, when would I use it, and when should one not use them ? i.e. how would I decide to protect some shared data structure with either a pthread mutex or a pthread spinlock ?
Lyke
  • 4,575
  • 6
  • 27
  • 26
41
votes
2 answers

How to restrict gdb debugging to one thread at a time

I want to debug a multi-threaded program by controlling which threads execute when. I am using C++ and gdb. I have two threads besides the main thread (for the example program) and I want to debug one thread while keeping the other stopped. Here is…
Yogeshwer Sharma
  • 3,647
  • 5
  • 25
  • 27
40
votes
3 answers

Do I need -D_REENTRANT with -pthreads?

On Linux (kernel 2.6.5) our build system calls gcc with -D_REENTRANT. Is this still required when using pthreads? How is it related to gcc -pthread option? I understand that I should use -pthread with pthreads, do I still need -D_REENTRANT? On a…
stefanB
  • 77,323
  • 27
  • 116
  • 141
40
votes
3 answers

How to continue one thread at a time when debugging a multithreaded program in GDB?

I have a program which uses two threads. I have put the break point in both the threads. While running the program under gdb I want to switch between the threads and make them run. (thread t1 is active and running and thread t2; when paused on the…
Arpit
  • 4,259
  • 10
  • 38
  • 43