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
95
votes
3 answers

Calling pthread_cond_signal without locking mutex

I read somewhere that we should lock the mutex before calling pthread_cond_signal and unlock the mutex after calling it: The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It…
B Faley
  • 17,120
  • 43
  • 133
  • 223
95
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,922
  • 14
  • 60
  • 85
90
votes
4 answers

Is it possible to determine the thread holding a mutex?

Firstly, I use pthread library to write multithreading C programs. Threads always hung by their waited mutexes. When I use the strace utility to find a thread in the FUTEX_WAIT status, I want to know which thread holds that mutex at that time. But I…
terry
  • 1,437
  • 4
  • 14
  • 11
86
votes
9 answers

efficient thread-safe singleton in C++

The usual pattern for a singleton class is something like static Foo &getInst() { static Foo *inst = NULL; if(inst == NULL) inst = new Foo(...); return *inst; } However, it's my understanding that this solution is not thread-safe,…
user168715
  • 5,469
  • 1
  • 31
  • 42
85
votes
4 answers

POSIX threads and signals

I've been trying to understand the intricacies of how POSIX threads and POSIX signals interact. In particular, I'm interested in: What's the best way to control which thread a signal is delivered to (assuming it isn't fatal in the first…
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
82
votes
5 answers

Pthreads vs. OpenMP

I'm creating a multi-threaded application in C using Linux. I'm unsure whether I should use the POSIX thread API or the OpenMP API. What are the pros & cons of using either? Edit: Could someone clarify whether both APIs create kernel-level or…
user191776
82
votes
9 answers

pthreads mutex vs semaphore

What is the difference between semaphores and mutex provided by pthread library ?
cppdev
  • 6,833
  • 13
  • 40
  • 45
76
votes
3 answers

understanding of pthread_cond_wait() and pthread_cond_signal()

Generally speaking, pthread_cond_wait() and pthread_cond_signal() are called as below: //thread 1: pthread_mutex_lock(&mutex); pthread_cond_wait(&cond, &mutex); do_something() pthread_mutex_unlock(&mutex); //thread…
user1944267
  • 1,557
  • 5
  • 20
  • 27
71
votes
4 answers

Preemptive threads Vs Non Preemptive threads

Can someone please explain the difference between preemptive Threading model and Non Preemptive threading model? As per my understanding: Non Preemptive threading model: Once a thread is started it cannot be stopped or the control cannot be…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
71
votes
9 answers

How to return a value from pthread threads in C?

I'am new to C and would like to play with threads a bit. I would like to return some value from a thread using pthread_exit() My code is as follows: #include #include void *myThread() { int ret = 42; …
Petr Peller
  • 8,581
  • 10
  • 49
  • 66
69
votes
3 answers

How to increase thread priority in pthreads?

I am using pthread in Linux. I would like to increase the thread priority by setting the parameters sched_param.priority. However, I could not find much info from the net regarding the range of the thread priority I could set, or about the…
Steveng
  • 1,141
  • 2
  • 11
  • 14
68
votes
5 answers

Can I get Unix's pthread.h to compile in Windows?

If I try to compile a program with #include in it, I get the error: pthread.h: No such file or directory Is it possible to get this to compile in a Windows environment? I am using Vista with the latest MinGW. I do not want to use the…
naspinski
  • 34,020
  • 36
  • 111
  • 167
67
votes
5 answers

How to set CPU affinity of a particular pthread?

I'd like to specify the CPU-affinity of a particular pthread. All the references I've found so far deal with setting the CPU-affinity of a process (pid_t) not a thread (pthread_t). I tried some experiments passing pthread_t's around and as expected…
Michael Wagner
65
votes
2 answers

Detached vs. Joinable POSIX threads

I've been using the pthread library for creating & joining threads in C. When should I create a thread as detached, right from the outset? Does it offer any performance advantage vs. a joinable thread? Is it legal to not do a pthread_join() on a…
user191776
65
votes
3 answers

How to set the name of a thread in Linux pthreads?

Is there any way of setting the name of a thread in Linux? My main purpose is it would be helpful while debugging, and also nice if that name was exposed through e.g. /proc/$PID/task/$TID/...
Anonym
  • 677
  • 1
  • 6
  • 5