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

How is stack size of Linux process related to pthread, fork and exec

I have a question about the stack size of a process on Linux. Is this stack size determined at linkage time and is coded in the ELF file? I wrote a program which prints its stack size by pthread_attr_getstacksize(&attr, &stacksize); And if I run…
Feng
  • 151
  • 1
  • 1
  • 3
15
votes
1 answer

gcc difference between -pthread and -pthreads?

I have a pthreads program. I have to compile it with gcc -pthread in Linux (-pthreads is unrecognized option) and gcc -pthreads in Sun (-pthread is unrecognized option). Why the difference, since it's the same compiler? However, -lpthread works on…
Steven
  • 2,538
  • 3
  • 31
  • 40
15
votes
4 answers

How to use PTRACE to get a consistent view of multiple threads?

While I was working on this question, I've come across a possible idea that uses ptrace, but I'm unable to get a proper understanding of how ptrace interacts with threads. Suppose I have a given, multithreaded main process, and I want to attach to a…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
15
votes
3 answers

change native thread priority on Android in c/c++

Insanely obscure pthread api for thread priority not only outlandishly incomprehensible, but also it just doesn't work on android. So, is there a way for me to reduce or increase a thread's priority? int currentPolicy; struct sched_param…
Pavel P
  • 15,789
  • 11
  • 79
  • 128
15
votes
4 answers

Mac/iPhone: Is there a way to get a thread identifier without using Objective-C?

Is there a way to get any kind of thread identifier of the currently running thread without resorting to Objective-C's NSThread. I'm improving our custom debug tracing system to handle multiple threads properly. For each line of trace output, I'd…
Teemu Kurppa
  • 4,779
  • 2
  • 32
  • 38
15
votes
9 answers

Linux Threads suspend/resume

I'm writing a code in which I have two threads running in parallel. 1st is the main thread which started the 2nd thread. 2nd thread is just a simple thread executing empty while loop. Now I want to pause / suspend the execution of 2nd thread by 1st…
ZeeAzmat
  • 1,461
  • 3
  • 12
  • 11
15
votes
1 answer

why does pthread_exit throw something caught by ellipsis?

if the function called by pthread_create has the following structure try{ ...code.... pthread_detach(pthread_self()); pthread_exit(NULL); }catch(...){ std::cout<<"I am here"<
Fabio Dalla Libera
  • 1,297
  • 1
  • 10
  • 24
15
votes
1 answer

Why glibc and pthread library both defined same APIs?

Why glibc and pthread library both defined same APIs ? Here is the snapshot ubuntu@ubuntu:/lib$ objdump -T /lib/i386-linux-gnu/libc.so.6 |grep pthread_cond_signal 000f8360 g DF .text 00000039 GLIBC_2.3.2 pthread_cond_signal 0012b940 g DF…
Lunar Mushrooms
  • 8,358
  • 18
  • 66
  • 88
15
votes
2 answers

race-condition in pthread_once()?

I have a std::future in one thread which is waiting on a std::promise being set in another thread. EDIT: Updated the question with an exemplar app which will block forever: UPDATE: If I use a pthread_barrier instead, the below code does not…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
15
votes
2 answers

pid for new thread

I have a quick question about new thread created by pthread_create(): When I print the pid (get from getpid()) of main thread and the child thread, they are the same while when I using htop linux utility to show pid, they are different. Can any one…
kai
  • 1,141
  • 3
  • 15
  • 25
14
votes
6 answers

How to use pthread_mutex_trylock?

Using trylock: FILE *fp; pthread_mutex_t demoMutex; void * printHello (void* threadId) { pthread_mutex_trylock (&demoMutex); pthread_t writeToFile = pthread_self (); unsigned short iterate; for (iterate = 0; iterate…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
14
votes
1 answer

Nice-Level for pthreads?

On an embedded system (Linux kernel 2.6.28 on ARM processor using glibc 2.6.1) I am running an application consisting of multiple threads. I would like one of those threads to get more CPU time than the others. One option for setting priorities…
Steven
14
votes
4 answers

What is the consensus number for semaphores?

(I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)?
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
14
votes
2 answers

POSIX Threads vs. Win32 Threads

I just dipped my toes into the POSIX pond and tried out POSIX threads for the first time. Until now, I'd been under the impression that there's a big architectural difference between POSIX threads and Win32 threads, but from the (admittedly little)…
user541686
  • 205,094
  • 128
  • 528
  • 886
14
votes
1 answer

Getting the saved instruction pointer address from a signal handler

My question is somewhat different from others that have asked about fault addresses. I'm trying to implement a horrible hack to determine, from a signal handler, whether the signal interrupted a syscall or ordinary user code by inspecting the code…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711