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

How does pthread implemented in linux kernel 3.2?

all, The code below comes from "Advanced Programing in Unix Environment", it creates a new thread, and prints the process id and thread id for main and new threads. In the book, it said that in linux, the output of this code would show that two…
jiluo
  • 2,296
  • 3
  • 21
  • 34
17
votes
3 answers

Pthread - What is the difference between time.h::sleep() and pthread.h::pthread_yield()?

I spent a good long while looking for info on the differences between time.h::sleep() and pthread.h::pthread_yield() but was unable to find any solid reference material and so I am posting this question. What is the difference between…
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
17
votes
6 answers

-lpthread option of gcc

As I know, if I want to use pthread library in linux environment I must include pthread.h and compile the source code with -lpthread option. But I don't understand why I should compile with -lpthread option. I think the option is redundant...…
David Johns
  • 1,201
  • 5
  • 15
  • 34
17
votes
8 answers

Passing structures as arguments while using pthread_create()

I tried passing a structure as the 4th argument while using pthread_create() with something like this: pthread_create(&tid1, NULL, calca, &t); //t is the struct Now whenever I try to access variables in the structure - t.a, t.b or t.c, I keep…
skinderneath
  • 269
  • 1
  • 4
  • 8
17
votes
3 answers

Posix thread tutorial

I'm looking for a comprehensive pthread tutorial. I considered buying Programming with posix threads but this book seems is bit dated. Other online tutorials like this are very basic. Can someone please suggest a good and complete pthread tutorial.
nik
  • 8,387
  • 13
  • 36
  • 44
17
votes
3 answers

pthread_cond_wait and mutex requirement

Why it is required to lock a mutex before calling pthread_cond_wait? Also, is it required to take a lock (on the same mutex) before calling pthread_cond_signal? thanks for your help.
aajtak
  • 269
  • 1
  • 3
  • 8
17
votes
2 answers

command to suspend a thread with GDB

I'm a little new to GDB. I'm hoping someone can help me with something that should be quite simple, I've used Google/docs but I'm just missing something. What is the 'normal' way folks debug threaded apps with GDB? I'm using pthreads. I'm wanting…
stuck
  • 2,264
  • 2
  • 28
  • 62
17
votes
2 answers

What happens to Mutex when the thread which acquired it exits?

Suppose there are two threads, the main thread and say thread B(created by main). If B acquired a mutex(say pthread_mutex) and it has called pthread_exit without unlocking the lock. So what happens to the mutex? Does it become free?
Sadish
  • 183
  • 1
  • 4
17
votes
5 answers

Why do I get "undefined reference" errors even when I include the right header files?

When I tried to compile this program, it failed: #include #include #include #include void *WriteNumbers(void *threadArg) { int start, stop; start = atoi((char *)threadArg); stop = start +…
Cold-Blooded
  • 420
  • 2
  • 8
  • 16
17
votes
4 answers

Is it OK to call pthread_exit from main?

When I call pthread_exit from main, the program never gets to terminate. I expected the program to finish, since I was exiting the program's only thread, but it doesn't work. It seems hung. #include #include #include…
user429788
  • 173
  • 1
  • 1
  • 4
17
votes
2 answers

Using pthread in c++

I am using pthread.h in a *.cc file. when I try to use pthread_exit(0); or pthread_join(mythrds[yy],NULL); it says: .cc:(.text+0x3e): undefined reference to `pthread_exit' when complied very similar code in a *.c file with gcc it work perfect. How…
ogzylz
  • 1,345
  • 2
  • 12
  • 13
17
votes
1 answer

Difference between pthread_exit, pthread_join and pthread_detach

I am complete new to pthreads and I wonder what the exact differences are. pthread_exit exits a thread. and thus pthread_join will return; However what does detach do that is different from pthread_join? for instance I create a thread and lets say…
user3021085
  • 407
  • 2
  • 5
  • 16
17
votes
2 answers

Thread Specific Data vs Thread Local Storage

I've read Kerrisk's The Linux Programming Interface: A Linux and UNIX System Programming Handbook, Chapter 31 on Threads. The chapter include Thread Specific Data (Section 31.3.4) and Thread Local Storage (Section 31.4). The topics were covered on…
jww
  • 97,681
  • 90
  • 411
  • 885
17
votes
2 answers

undefined reference to `pthread_mutex_trylock'

I have the following test program. #include #include using namespace std; pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; int main(int argc, char *argv[]) { int iret; iret = pthread_mutex_trylock( & mymutex ); …
enzo1959
  • 409
  • 2
  • 5
  • 13
17
votes
2 answers

C pthread mutex: Expected expression before `{'

I am using the pthread library to create two threads. I am using two queues to communicate the data between the two threads (producer-consumer) and hence want to have a mutex to sync the push-pops in the queue by the threads. But I get a compile…
aoak
  • 983
  • 1
  • 11
  • 20