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

when g++ static link pthread, cause Segmentation fault, why?

#include #include #include #define SIZE 1024 #define AMOUNT 100000 #define THREADS 4 class A { private: char a[SIZE]; }; void test() { std::cout << "test start\n"; std::map container; for(int…
Yueyoum
  • 2,823
  • 5
  • 23
  • 26
39
votes
4 answers

Mutex lock threads

Am new to multi threaded/processs programming. So here's what I need to clarify. Process A code pthread_mutex_lock() pthread_create(fooAPI(sharedResource)) //fooAPI creates another thread with shared resource that shares across…
resting
  • 16,287
  • 16
  • 59
  • 90
38
votes
1 answer

What does "Performing Test CMAKE_HAVE_LIBC_PTHREAD" failed actually mean?

The cmake partial output looks like this: -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
Neo
  • 676
  • 1
  • 4
  • 12
38
votes
4 answers

Existing threadpool C implementation

What open-source implementation(s) in C for a pthreads thread pool would you recommend ? Additional points if this implementation is : Light-weight: glib, APR, NSPR and others come with a big buy-in, I'd rather have just 2 files (header and…
Mathias Brossard
  • 3,668
  • 2
  • 26
  • 30
38
votes
1 answer

How to get CMake to recognize pthread on Ubuntu?

If I compile on the command-line with g++ directly, I can see everything I need is there: $ g++ -pthread test.cpp $ ldd a.out linux-vdso.so.1 => (0x00007fffd05b3000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6…
Stéphane
  • 19,459
  • 24
  • 95
  • 136
37
votes
3 answers

Does guarding a variable with a pthread mutex guarantee it's also not cached?

Consider a simple (global in my case) variable: int i; Somewhere this variable is accessed pthread_mutex_lock(i_mutex); if(i == other value) { do_something(); } pthread_mutex_unlock(i_mutex); Another thread updates i while it holds i_mutex .…
Anonym
  • 7,345
  • 8
  • 35
  • 32
37
votes
1 answer

How to prevent writer starvation in a read write lock in pthreads

I have some questions regarding read-write locks in POSIX Pthreads on a *nix system, say Linux for example. I wish to know what is the default bias for read write lock i.e does it prefer reads over writes or vice versa ? Does it provide some api to…
ghayalcoder
  • 1,675
  • 2
  • 17
  • 24
36
votes
13 answers

Using C/Pthreads: do shared variables need to be volatile?

In the C programming language and Pthreads as the threading library; do variables/structures that are shared between threads need to be declared as volatile? Assuming that they might be protected by a lock or not (barriers perhaps). Does the pthread…
fuad
  • 4,265
  • 9
  • 34
  • 32
36
votes
5 answers

How can I wait for any/all pthreads to complete?

I just want my main thread to wait for any and all my (p)threads to complete before exiting. The threads come and go a lot for different reasons, and I really don't want to keep track of all of them - I just want to know when they're all…
Brad
  • 11,262
  • 8
  • 55
  • 74
36
votes
2 answers

Why does start_routine for pthread_create return void* and take void*

The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); I understand it all…
ldog
  • 11,707
  • 10
  • 54
  • 70
35
votes
5 answers

What is the _REENTRANT flag?

which compiling a multithreaded program we use gcc like below: gcc -lpthread -D_REENTRANT -o someprogram someprogram.c what exactly is the flag -D_REENTRANT doing over here?
Vijay
  • 65,327
  • 90
  • 227
  • 319
34
votes
7 answers

Do mutexes guarantee ordering of acquisition? Unlocking thread takes it again while others are still waiting

A coworker had an issue recently that boiled down to what we believe was the following sequence of events in a C++ application with two threads: Thread A holds a mutex. While thread A is holding the mutex, thread B attempts to lock it. Since it is…
Jason R
  • 11,159
  • 6
  • 50
  • 81
34
votes
4 answers

What is the difference between Go's multithreading and pthread or Java Threads?

What is the difference between Go's multithreading approach and other approaches, such as pthread, boost::thread or Java Threads?
Frank
  • 64,140
  • 93
  • 237
  • 324
33
votes
4 answers

Set CPU affinity when create a thread

I want to create a C++11 thread which I want it to run on my first core. I find that pthread_setaffinity_np and sched_setaffinity can change the CPU affinity of a thread and migrate it to the specified CPU. However this affinity specification…
Peng Zhang
  • 3,475
  • 4
  • 33
  • 41
33
votes
1 answer

When to use pthread condition variables?

pthread question: it appears that a condition variable only works if pthread_cond_wait is called before the other thread calls pthread_cond_notify. If notify somehow happens before wait then wait will be stuck. My question is: when should condition…
MichaelMoser
  • 3,172
  • 1
  • 26
  • 26