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

In C, how do I pass variables to functions in Pthreads upon thread creation?

Working from this example: https://computing.llnl.gov/tutorials/pthreads/samples/hello.c I've worked backwards and tried to edit in what I'm hoping to accomplish. I'd like to pass data to the thread being spawned. #include #include…
Volumetricsteve
  • 213
  • 1
  • 7
3
votes
2 answers

signal handler in child thread

I tried to install SIGINT handler for the child thread in the code below. I expect the child thread to print hello when it receives SIGINT from the parent process. However, nothing comes out and the program exits immediately. #include…
HuangJie
  • 1,488
  • 1
  • 16
  • 33
3
votes
3 answers

Linux 2.6.30 uClibc 0.9.29 pthread_create two tread insted just one

Absolutely strange situation on thread creation , here the test code : #include /* C99 types */ #include /* bool type */ #include /* printf, fprintf, snprintf, fopen, fputs */ #include…
queifaro
  • 41
  • 3
3
votes
2 answers

Pass argument to multiple threads

New to C, I was reading here about how to properly pass an argument to a thread. What about if this argument needed to be passed to multiple threads? Where/how should I use free()? Say: void *foo(void *i) { int a = *((int *) i); while(1){ …
Stephan GM
  • 245
  • 3
  • 15
3
votes
3 answers

A function in a pthread

Can I call another function in a thread runner function, called by a pthread_create()? Are there any restrictions on such functions?
user191776
3
votes
1 answer

setjmp/longjmp between threads to handle timeout

I'm porting a software from an embedded computer to a Linux machine. (Ubuntu 14.04 or Raspbian (raspberry pi)) The original program was using setjmp/longjmp to handle timeout and CTRL+C event. It was running on a Microcontroller with a single main…
ssinfod
  • 975
  • 2
  • 15
  • 31
3
votes
1 answer

pthread with php 7.0.8 (ZTS)

I would like use pthreads with php 7.0.8 (ZTS) (Manually compiled and configured) I have add the followinG configuration during compiling of php : --enable-maintainer-zts \ --enable-pthreads=shared \ --with-tsrm-pthreads \ NOTICE: fpm is running,…
Dev Loots
  • 708
  • 9
  • 28
3
votes
2 answers

Do C++11 threads provide a way for detached threads to continue after the main thread exits?

Normally, when main() exits, all threads are killed. pthread_exit(3) says To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3). Is there an equivalent C++11 API call? …
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
3
votes
5 answers

concurrent queue in C++

I am trying to design a queue which could be simultaneously accessed by multiple read/write threads. I prefer using 2 mutexes, one apiece for read and write. Doing write is simple enough, lock the write mutex, append data, unlock and you are done.…
Fanatic23
  • 3,378
  • 2
  • 28
  • 51
3
votes
3 answers

Trouble using pthread_mutex_lock

I just started at college to study a little bit about threads, and it seems that I don't quite get the hang of it. I wanted for my code to get the arguments and check if they are either even or prime numbers, and if they are, to print them.…
Darkmer
  • 45
  • 3
3
votes
3 answers

pthread mutex locking variables used in statements

First of all, I have a gut feeling that says, inside an if statement, if I am using the variable, it counts as reading the variable so I should lock it with mutex there also (if another pthread might be doing stuff with it). Am I right that I should…
etugcey
  • 109
  • 2
  • 11
3
votes
1 answer

sched_getcpu doesn't work

I have a virtual machine on Google cloud with 1 CPU socket with 16 cores and 2 threads per core (hyper-threading). This is the output of lscpu: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little…
eladm26
  • 517
  • 4
  • 23
3
votes
1 answer

why can't I cancel pthreads while waiting on a condition, multiple times?

In my program, pthreads are spawned and wait for conditions, and may be cancelled while waiting. This works for a while but then stops working - the thread cannot acquire the mutex anymore. Below is the SSCCE. The routine code follows well-known…
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
3
votes
1 answer

What is the best way to use pthread and mutex lock to protect a memory when it is modified by one thread and read by other threads?

I am writing a program in which a memory array is modified by one thread under 2 possible operations (modify the array content, or dealloc the array and replace it by allocating a new array). The memory array can be read by many threads except when…
Pippi
  • 2,451
  • 8
  • 39
  • 59
3
votes
0 answers

Address Sanitizer thread limit exceeded

I am profiling a program compiled with gcc 6.1 with -fsanitize=address option. The program is multi-threaded with clean exits for each thread (with pthread_exit). Address Sanitizer fails with the message: ==16800==AddressSanitizer: Thread limit…
Amitabha
  • 83
  • 5