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

Share condition variable & mutex between processes: does mutex have to locked before?

I need to some little help to understand how to use condition variables in C to resolve an exercise. Here is a little example: #include #include #include #include #include #include…
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
26
votes
3 answers

How do I determine if a detached pthread is alive?

How do I determine if a detached pthread is still alive ? I have a communication channel with the thread (a uni-directional queue pointing outwards from the thread) but what happens if the thread dies without a gasp? Should I resign myself to using…
jldupont
  • 93,734
  • 56
  • 203
  • 318
25
votes
3 answers

Do pthread mutexes work across threads if in shared memory?

I found this: Fast interprocess synchronization method I used to believe that a pthread mutex can only be shared between two threads in the same address space. The question / answers there seems to imply: If I have two separate proceses A & B. They…
anon
  • 41,035
  • 53
  • 197
  • 293
25
votes
5 answers

why pthread causes a memory leak

Whenever I create a pthread, valgrind outputs a memory leak, For example the below code: #include #include #include void *timer1_function (void *eit){ (void) eit; printf("hello world\n"); …
Johan Elmander
  • 646
  • 2
  • 8
  • 11
24
votes
4 answers

Why do you need a while loop while waiting for a condition variable

Say you have this code pthread_mutex_lock(&cam->video_lock); while(cam->status == WAIT_DISPLAY) // <-- Why is this a 'while' and not an 'if'? pthread_cond_wait(&cam->video_cond, &cam->video_lock); pthread_mutex_unlock(&cam->video_lock); My…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
24
votes
8 answers

Non-blocking pthread_join

I'm coding the shutdown of a multithreaded server.If everything goes as it should all the threads exit by their own, but there's a small chance that a thread gets stuck.In this case it would be convenient to have a non-blocking join so I could…
Figo
  • 265
  • 1
  • 2
  • 7
24
votes
2 answers

C , how to create thread using pthread_create function

I'm making a c file for a dispatch queue that gets a task and put it in to a queue which is the linked list. In order to do this, I need to create threads using pthread_t cThread; if(pthread_create(&cThread, NULL, work, param)){ perror("ERROR…
Leanne
  • 667
  • 3
  • 11
  • 23
24
votes
2 answers

How to utilize a thread pool with pthreads?

I have a queue of jobs and I want to make a pool of four threads where I can throw my jobs at. What I am stuck at is in how to make the threads and keep them suspended while there is no work. JOB QUEUE | job1 | job2 | job3 | job4 |…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
24
votes
5 answers

pthread_detach question

Till recently, I was under the impression that if you "detach" a thread after spawning it, the thread lives even after the "main" thread terminates. But a little experiment (listed below) goes contrary to my belief. I expected the detached thread…
puffadder
  • 1,814
  • 3
  • 20
  • 32
24
votes
2 answers

Can 2 pthread condition variables share the same mutex?

I went through the documentation in http://www.opengroup.org/onlinepubs/009695399/functions/pthread_cond_wait.html but this is not mentioned explicitly. Any prompt response will be very appreciated.
Fanatic23
  • 3,378
  • 2
  • 28
  • 51
24
votes
2 answers

return() versus pthread_exit() in pthread start functions

The following program shows that we can use return or pthread_exit to return a void* variable that is available to pthread_join's status variable. Should there be a preference for using one over the other? Why does using return work? Normally we…
ValenceElectron
  • 2,678
  • 6
  • 26
  • 27
24
votes
2 answers

pthread_t to gdb thread id

Does anyone know a way to go from a pthread_t to what GDB displays with info threads? So I have: (gdb) info threads 37 Thread 22887 0xb7704422 in __kernel_vsyscall () 36 Thread 22926 0xb7704422 in __kernel_vsyscall () 35 Thread 22925 …
Steven Behnke
  • 3,336
  • 3
  • 26
  • 34
24
votes
4 answers

Fork and core dump with threads

Similar points to the one in this question have been raised before here and here, and I'm aware of the Google coredump library (which I've appraised and found lacking, though I might try and work on that if I understand the problem better). I want…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
24
votes
5 answers

How to measure mutex contention?

I have some threaded code using PThreads on Linux that, I suspect, is suffering from excessive lock contention. What tools are available for me to measure this? Solaris has DTrace and plockstat. Is there something similar on Linux? (I know about a…
Luís Oliveira
  • 2,964
  • 1
  • 20
  • 26
23
votes
5 answers

Is it always safe to convert an integer value to void* and back again in POSIX?

This question is almost a duplicate of some others I've found, but this specifically concerns POSIX, and a very common example in pthreads that I've encountered several times. I'm mostly concerned with the current state of affairs (i.e., C99 and…
Quantumboredom
  • 1,426
  • 11
  • 19