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

How to terminate threads cleanly in C?

I am trying to write a multithreaded application in C for the Raspberry Pi in raspbian environment (UNIX system). Apart from the main thread three other threads are created and do the following: the first looks at the output of a PIR sensor and if…
roschach
  • 8,390
  • 14
  • 74
  • 124
3
votes
2 answers

pthread clean up handler

I have some dynamic allocations which I want to make sure are freed when the thread exits/terminates. Please consider the following scenario: static void thread_cleanup_handler(void* arg) { free(arg); } static void* threadFunction(void* arg) { …
Tsef
  • 1,018
  • 9
  • 22
3
votes
4 answers

Threading In C: Producer Consumer taking forever to run

I'm new to concept of threading. I was doing producer consumer problem in C but the consumer thread doesn't run when parallel with producer. my code is as follows: #include #include #include int S; int E; int…
Prajval M
  • 2,298
  • 11
  • 32
3
votes
3 answers

Pthread: lock PTHREAD_MUTEX_ERRORCHECK mutex from non-owner thread

classical question please; I didn't find confirmation from code; C language. I'm running the below code on Windows. /* This is an implementation of the threads API of POSIX 1003.1-2001.*/ #include #include #include…
BayE
  • 75
  • 2
  • 6
3
votes
1 answer

RPC Multithreaded Server on Linux - Pthreads

I am working on a RPC project for university. I have to implement a RPC multithreaded Server. I've been working on a server with only one procedure that sums two numbers. My code is at: https://github.com/alvmatias/pdytr This is what i do to handle…
Matias
  • 51
  • 4
3
votes
1 answer

atomicity of pthread_cond_wait()'s unlock-and-wait?

How atomic is the unlock-and-wait of pthread_cond_wait() call? Is there a window where mutex is already unlocked but the thread is not yet in the part where it is actually waiting and able to receive notifications? IOW, is there a potential for…
wilx
  • 17,697
  • 6
  • 59
  • 114
3
votes
2 answers

How to enforce memory ordering with gcc on x86

I want to share a data struct between threads (gcc, Linux, x86). Let's say I have the following code in thread A: shared_struct->a = 1; shared_struct->b = 1; shared_struct->enable = true; Thread B is a periodic task that checks that struct first…
filo
  • 223
  • 3
  • 14
3
votes
0 answers

Pthreads & Libcurl - Segmentation Fault when using Authorization header

I have been tearing my hair out for the past 3 days, trying to figure out why my C++ code outputs a "Segmentation fault". Being new to C++ didn't help either. Nor did using pthreads help with narrowing down with the problem (If you aren't able to…
3
votes
0 answers

Profiling cache misses for separate pthread using PAPI

I am trying to investigate the performance of my program, whereas cache misses is a huge bottleneck. For testing purposes, before implementing PAPI in to the target application, I needed to verify how stuff works, which is why I posted a sample…
Jakob Danielsson
  • 767
  • 1
  • 8
  • 16
3
votes
2 answers

What happens if a thread in C tries to acquire a lock it already has?

So assuming 1 thread, the thread acquires a lock via: pthread_mutex_lock(&lock); then before unlocking, it again reaches a line that is: pthread_mutex_lock(&lock); Will the pthread library block the advancement of the thread, or will it recognize…
Fuad
  • 1,419
  • 1
  • 16
  • 31
3
votes
1 answer

Giving a function argument dynamically in C++

I have the following code where I create threads based on the for loop below, the 3rd argument in pthread takes a void* ()(void) and I am giving it a string arg. The problem is that every thread has it's own method i.e thread 0 has its method…
mnismael
  • 43
  • 5
3
votes
0 answers

Get all thread-ids of a running process with PThreads

I trying to debug a Pthread enabled program under Linux or FreeBSD. Thereby I require the id's (in terms of pthread_t thread identifiers) of all threads running in the program. For example as an array of pthread_t. But I am not able to modify the…
M.K. aka Grisu
  • 2,338
  • 5
  • 17
  • 32
3
votes
2 answers

get pthread_t from thread id

I am unable to find a function to convert a thread id (pid_t) into a pthread_t which would allow me to call pthread_cancel() or pthread_kill(). Even if pthreads doesn't provide one is there a Linux specific function? I don't think such a function…
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
3
votes
0 answers

C pthread_create causing memory leak

I have a multi-threaded application which uses joinable threads. In order to create the threads (which are in total nthreads workers+1 additional thread) I use a regular call to pthread_create. However, when I try to debug memory leaks, I get a x…
BattiestFawn66
  • 67
  • 3
  • 10
3
votes
3 answers

Multithreaded (C) program threads not terminating

I am trying to finish up a program that uses multiple threads (3) to distribute a hypothetical scholarship of $4000. Each time a thread processes, it "locks" the "critical section" and blocks the other threads from taking out their chunk from the…
Nathan1324
  • 158
  • 1
  • 2
  • 12