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

Will calling select()/pselect() in secondary thread cause primary thread to block?

I have an application that I'm working on that requires a couple of secondary threads, and each will be responsible for a number of file handles (at least 1, upwards of 10). The file handles are not shared amongst the threads, so I don't have to…
Will
  • 3,500
  • 4
  • 30
  • 38
3
votes
2 answers

pthread_cancel() on linux leads to exception/coredump, why?

I was testing the behavior of how pthread_cancel works. #include #include #include using namespace std; int retval=70; void* tf(void*arg){ int oldstate; int i=0; …
Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
3
votes
1 answer

How should a PHP thread store its data?

So I have been googling and reading up and down the internet about PHP pthreads3 and how they are supposed to store data. (Or rather, how they are not) It seems to me that the only way for a thread to store its data properly is to create a new…
JohnF
  • 98
  • 9
3
votes
3 answers

Multithreading in Embedded Systems

I am confused about the following: I am hoping to get a job in the field of embedded systems. However, every interview I've had seems to end up with a conversation about threads in C and how to do thread-safe programming My question is how do I go…
3
votes
2 answers

Inconsistent runtimes with pthreads

My multithreaded C program runs the following routine : #define NUM_LOOP 500000000 long long sum = 0; void* add_offset(void *n){ int offset = *(int*)n; for(int i = 0; i
Aroonalok
  • 631
  • 1
  • 7
  • 18
3
votes
1 answer

POSIX Threads and SIGSEGV

I have a system with 10+ threads. I have a signal handler to catch SIGSEGV. if one thread generates SIGSEGV, does that signal go to all threads, or just to the thread that generated the signal?
vy32
  • 28,461
  • 37
  • 122
  • 246
3
votes
1 answer

When will a thread woken via a condition variable run?

When I wake up a thread waiting on a condition variable while holding the corresponding mutex, can I assume that the woken thread will run after I release the mutex and before anyone else (myself included) can lock the mutex again? Or can I only be…
Carsten S
  • 207
  • 3
  • 14
3
votes
2 answers

What happens with unjoined and not detached threads when the whole process terminates?

It is expected that threads, on which pthread_detach() was not called, should be pthread_join()ed before the main thread returns from main() or calls exit(). However, what happens when this requirement is not met? What happens when a process…
user4385532
3
votes
2 answers

Mysterious segfaults when overriding pthread functions on glibc but not on musl

I'm trying to override pthread_create and pthread_exit. The overrides should call the originals. I can override pthread_create, and it appears to works as long as I exit my main thread with pthread_exit(0);. If I don't it segfaults. If I even…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
3
votes
0 answers

Process is stuck when generating core with gcore

I have a multi-threaded process for which I wanted to generate core dump. gcore ran, gdb started, the process went to "t" state according to ps. However, it got stuck there. As it was already being traced, I could not attach another gdb session to…
learner
  • 93
  • 8
3
votes
1 answer

Using the heap in a pthread allocates >100MB of RAM

A simple pthread test case allocates the following RAM as measured by the VIRT column of top: No pthread/heap usage: 4224 kB Use pthread but no heap: 14716 kB Use pthread and heap in main (but not thread): 23632 kB Use pthread and heap in main and…
Chris Fryer
  • 726
  • 3
  • 7
3
votes
2 answers

checking whether fork() is safe

fork()'s behaviour is undefined if there are multiple threads in the process. How can I check that there is only a single thread (on linux primarily, but windows, darwin are also of interest)?
melisgl
  • 308
  • 2
  • 13
3
votes
5 answers

Is it thread-safe to strcmp?

strcmp(variable, "constant"); Or do I have to protect it with a mutex?
j riv
  • 3,593
  • 6
  • 39
  • 54
3
votes
2 answers

Define the size of a global array from the command line

I am doing an assignment where I need to use pthreads or semaphores to synchronize some processes which access some shared resource. Since all of our examples in class use a global variable as the shared resource I planned on doing the same thing,…
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
3
votes
2 answers

How I can keep alive the process while one of its threads is killed using pthread in C++?

I have write a program with 3 threads using pthread in c++, and I want to keep alive the process while the thread 1 is killed. here is my program: #include #include #include using namespace std; int a = 0; void…