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
13
votes
4 answers

Getting EPERM when calling pthread_create() for SCHED_FIFO thread as root on Linux

I am trying to spawn threads with SCHED_FIFO or SCHED_RR policies as root on a Linux system but my calls to pthread_create() are returning 1 (EPERM). The man page for pthread_create() says that EPERM indicates that "[t]he caller does not have …
ChrisL
  • 133
  • 1
  • 1
  • 5
13
votes
2 answers

Undefined reference to 'pthread_create' — linker command option order (libraries before/after object files?)

When I try to compile that, I receive a particular error. But, it's not possible because I use the right flag. In server.c there is the library pthread.h. So, how can I resolve my linking problem? I'm using Linux (Ubuntu). make gcc -c -Wall…
rschirin
  • 1,939
  • 10
  • 34
  • 44
13
votes
2 answers

How to pass multiple parameters to a thread in C

I am trying to pass two parameters to a thread in C. I have created an array (of size 2) and am trying to pass that array into the thread. Is this the right approach of passing multiple parameters into a thread ? // parameters of input. These are…
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
13
votes
1 answer

custom RAII C++ implementation for scoped mutex locks

I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex. In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked. Any…
cateof
  • 6,608
  • 25
  • 79
  • 153
13
votes
2 answers

Memory not freed but still reachable, is it leaking?

By checking with valgrind, I see that 5 blocks of memory were not freed after terminating my program, but they are still reachable. Do I need to be bothered by it? And how it happens? zhanwu@gelata:~/sandbox$ valgrind ./a.out ==2430== Memcheck, a…
zhanwu
  • 1,508
  • 5
  • 16
  • 27
13
votes
3 answers

Dealing With Asynchronous Signals In Multi Threaded Program

The Linux Programming Interface Book has mentioned a method for dealing with asynchronous signals in a multi threaded program: All threads block all of the asynchronous signals that the process might receive. The simplest way to do this is to…
Majid Azimi
  • 5,575
  • 13
  • 64
  • 113
13
votes
6 answers

Implementing a FIFO mutex in pthreads

I'm trying to implement a binary tree supporting concurrent insertions (which could occur even between nodes), but without having to allocate a global lock or a separate mutex or mutexes for each node. Rather, the quantity of such locks allocated…
ManRow
  • 1,563
  • 4
  • 21
  • 40
13
votes
4 answers

C linux pthread thread priority

My program has one background thread that fills and swaps the back buffer of a double buffer implementation. The main thread uses the front buffer to send out data. The problem is the main thread gets more processing time on average when I run the…
eat_a_lemon
  • 3,158
  • 11
  • 34
  • 50
13
votes
6 answers

pthread Library location

In which directory does the libpthread library reside on a Linux system ?
Dew
  • 2,993
  • 5
  • 17
  • 7
13
votes
4 answers

PThreads & MultiCore CPU on Linux

I am writing a simple application that uses Threads to increase the performance. The problem is, that this application runs fine on windows, using the 2 cores that my CPU has. But When I execute on Linux, It seems that only uses 1 Core. I can't…
IRTHUS
  • 133
  • 1
  • 5
13
votes
1 answer

Why the second argument to pthread_join() is a **, a pointer to a pointer?

I am new to using pthread and also not that familiar with pointers to pointers. Could somebody perhaps explain why the second argument of pthread_join() is a void **. Why is it designed like this. int pthread_join(pthread_t thread, void…
artic sol
  • 349
  • 6
  • 18
13
votes
4 answers

Send and catch signals to pthreads in C

I know how to send signals to child process in C using the kill(pid_t pid, int sig) function. What about sending signals to threads? is it possible?. If so, how to catch signals on the "child" thread. For example, if the main thread sends me a…
gvalero87
  • 855
  • 3
  • 15
  • 32
13
votes
5 answers

concurrent linked list

I am trying to design a linked list in c++ that allows concurrent access. Clearly using a single lock for this list is grossly inefficient since disjoint areas may be updated in parallel. Now what are my options other than storing a lock per node?…
Fanatic23
  • 3,378
  • 2
  • 28
  • 51
13
votes
2 answers

In pthread, how to reliably pass signal to another thread?

I'm trying to write a simple thread pool program in pthread. However, it seems that pthread_cond_signal doesn't block, which creates a problem. For example, let's say I have a "producer-consumer" program: pthread_cond_t my_cond =…
kizzx2
  • 18,775
  • 14
  • 76
  • 83
13
votes
3 answers

How to understand "/proc/[pid]/stack"?

According to proc manual: /proc/[pid]/stack (since Linux 2.6.29) This file provides a symbolic trace of the function calls in this process's kernel stack. This file is provided only if the kernel was built with…
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164