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
19
votes
5 answers

recv() is not interrupted by a signal in multithreaded environment

I have a thread that sits in a blocking recv() loop and I want to terminate (assume this can't be changed to select() or any other asynchronous approach). I also have a signal handler that catches SIGINT and theoretically it should make recv()…
Alex B
  • 82,554
  • 44
  • 203
  • 280
19
votes
3 answers

POSIX API call to list all the pthreads running in a process

I have a multi-threaded application in a POSIX/Linux environment - I have no control over the code that creates the pthreads. At some point the process - owner of the pthreads - receives a signal. The handler of that signal should abort,cancel or…
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
19
votes
3 answers

CPU Affinity Masks (Putting Threads on different CPUs)

I have 4 threads, and I am trying to set thread 1 to run on CPU 1, thread 2 on CPU 2, etc. However, when I run my code below, the affinity masks are returning the correct values, but when I do a sched_getcpu() on the threads, they all return that…
hwrd
  • 2,134
  • 6
  • 29
  • 36
19
votes
5 answers

What does the GDB backtrace message "0x0000000000000000 in ?? ()" mean?

What does it mean when it gives a backtrace with the following output? #0 0x00000008009c991c in pthread_testcancel () from /lib/libpthread.so.2 #1 0x00000008009b8120 in sigaction () from /lib/libpthread.so.2 #2 0x00000008009c211a in…
Shane Breatnach
  • 934
  • 2
  • 8
  • 14
19
votes
4 answers

When is clone() and fork better than pthreads?

I am beginner in this area. I have studied fork(), vfork(), clone() and pthreads. I have noticed that pthread_create() will create a thread, which is less overhead than creating a new process with fork(). Additionally the thread will share file…
Brijesh Valera
  • 1,085
  • 2
  • 9
  • 30
18
votes
4 answers

Is mutex needed to synchronize a simple flag between pthreads?

Let's imagine that I have a few worker threads such as follows: while (1) { do_something(); if (flag_isset()) do_something_else(); } We have a couple of helper functions for checking and setting a flag: void flag_set() {…
snap
  • 2,751
  • 22
  • 33
18
votes
3 answers

Tool for tracing and visualisation of pthread behaviour in Linux

I am eager to find a tool that allows me to trace the behaviour of the pthreads in a program I am working on. I am aware that there where similar questions asked before, see here and here . As it turns out, the tools that are recommended are not…
techshack
  • 359
  • 2
  • 18
18
votes
1 answer

Cost of context switch between threads of same process, on Linux

Is there any good empirical data on the cost of context switching between threads of the same process on Linux (x86 and x86_64, mainly, are of interest)? I'm talking about the number of cycles or nanoseconds between the last instruction one thread…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
18
votes
2 answers

Static pthreads mutex initialization

Using pthreads, how would one, in C, initialize a static array of mutexes? For a single static mutex, it seems I can use PTHREAD_MUTEX_INITIALIZER. But what about an static array of them? As, in for example, #include <pthread.h> #define…
ManRow
  • 1,563
  • 4
  • 21
  • 40
18
votes
1 answer

Program compiled with -fPIC crashes while stepping over thread-local variable in GDB

This is a very strange problem which occurs only when the program is compiled with -fPIC option. Using gdb I'm able to print thread local variables but stepping over them leads to crash. thread.c #include #include #include…
Kartik Anand
  • 4,513
  • 5
  • 41
  • 72
18
votes
4 answers

pthreads: reader/writer locks, upgrading read lock to write lock

I'm using read/write locks on Linux and I've found that trying to upgrade a read locked object to a write lock deadlocks. i.e. // acquire the read lock in thread 1. pthread_rwlock_rdlock( &lock ); // make a decision to upgrade the lock in threads…
ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43
18
votes
4 answers

Threading problems with GTK

I'm building a fairly simple C application using GTK, but have to perform some blocking IO which will trigger updates to the GUI. In order to do this, I start a new pthread right before gtk_main() as such: /* global variables */ GMainContext…
Jon Gjengset
  • 4,078
  • 3
  • 30
  • 43
18
votes
1 answer

Pthread Mutex: pthread_mutex_unlock() consumes lots of time

I wrote a multi-thread program with pthread, using the producer-consumer model. When I use Intel VTune profiler to profile my program, I found the producer and consumer spend lots of time on pthread_mutex_unlock. I don't understand why this…
lei_z
  • 1,049
  • 2
  • 13
  • 27
18
votes
3 answers

is it necessary to call pthread_mutex_destroy on a mutex?

I am using pthread_mutex_t in a C++ program, as follows: class Mutex : public noncopyable { public: Mutex() { pthread_mutex_init(&m_mutex, NULL); } void acquire() { pthread_mutex_lock(&m_mutex); } void…
Wayne Uroda
  • 5,025
  • 4
  • 30
  • 35
18
votes
3 answers

CMake : not found in Windows

I'm newbie with CMake. I tested it on Linux for a program I am making. This program uses (POSIX Threads lib), so in my CMakeList, I added : find_package(Threads) It works on Linux distribs (Arch, Mint, Ubuntu, ...), but now, I'm trying it in…
Neozaru
  • 1,109
  • 1
  • 10
  • 25