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
22
votes
6 answers

Simple pthread! C++

I have no idea why this doesn't work #include #include using namespace std; void *print_message(){ cout << "Threading\n"; } int main() { pthread_t t1; pthread_create(&t1, NULL, &print_message, NULL); …
Angel.King.47
  • 7,922
  • 14
  • 60
  • 85
22
votes
5 answers

How to profile pthread mutex in linux?

I would like to know how to profile a pthread mutex to see if there are any locking contention points in my code. (who likes contentious code, right? :) I know how to do a more general profiling of the code, as I mention here. But I would like to…
Brady
  • 10,207
  • 2
  • 20
  • 59
22
votes
7 answers

is it necessary to call pthread_join()

I create more than 100 threads from my main() so I just wanted to know that do I need to call pthread_join() before I exit my main(). Also, I do not need the data generated by these threads, basically, all the threads are doing some job independent…
nav_jan
  • 2,473
  • 4
  • 24
  • 42
21
votes
2 answers

Which thread handles the signal?

I have 2 threads(thread1 and thread2). And I have signal disposition for SIGINT. Whenever SIGINT occurs thread 2 should handle the signal. For that I wrote below program #include #include #include void sig_hand(int…
gangadhars
  • 2,584
  • 7
  • 41
  • 68
21
votes
4 answers

Is it safe to mix pthread.h and C++11 standard library threading features?

Can I spawn a thread with pthread_create and use std::mutex inside of it safely? I would think that if std::mutex is implemented as a pthread_mutex_t then it would be fine but I don't see this documented anywhere For example: #include…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
21
votes
5 answers

CLOCK_MONOTONIC and pthread_mutex_timedlock / pthread_cond_timedwait

The pthread_mutex_timedlock documentation says that abs_timeout takes a CLOCK_REALTIME. However, we all know that it is inappropriate for timing a specific duration (due to system time adjustments). Is there a way to make pthread lock timeout on…
Zach Saw
  • 4,308
  • 3
  • 33
  • 49
21
votes
2 answers

Why do we need a condition check before pthread_cond_wait

I am trying to learn basics of pthread_cond_wait. In all the usages, I see either if(cond is false) pthread_cond_wait or while(cond is false) pthread_cond_wait My question is, we want to cond_wait only because condition is false. Then why…
CHID
  • 1,625
  • 4
  • 24
  • 43
20
votes
3 answers

Pthread Mutex lock unlock by different threads

A Naive question .. I read before saying - "A MUTEX has to be unlocked only by the thread that locked it." But I have written a program where THREAD1 locks mutexVar and goes for a sleep. Then THREAD2 can directly unlock mutexVar do some operations…
codingfreak
  • 4,467
  • 11
  • 48
  • 60
20
votes
4 answers

For pthread, How to kill child thread from the main thread

I use pthread_create to create several child threads. At a time, the main thread wants to kill all child threads or there will be segment falut. Which function should I use to finish that? I searched the answer from google and got function like…
terry
  • 1,437
  • 4
  • 14
  • 11
20
votes
6 answers

Does pthreads provide any advantages over GCD?

Having recently learned Grand Central Dispatch, I've found multithreaded code to be pretty intuitive(with GCD). I like the fact that no locks are required(and the fact that it uses lockless data structures internally), and that the API is very…
Mike
  • 23,892
  • 18
  • 70
  • 90
20
votes
2 answers

How to sleep or pause a PThread in c on Linux

I am developing an application in which I do multithreading. One of my worker threads displays images on the widget. Another thread plays sound. I want to stop/suspend/pause/sleep the threads on a button click event. It is same as when we click on…
Muhammad Ummar
  • 3,541
  • 6
  • 40
  • 71
20
votes
5 answers

Equivalent of SetThreadPriority on Linux (pthreads)

Given the following bit of code, I was wondering what the equivalent bit of code would be in linux assuming pthreads or even using the Boost.Thread API. #include int main() { …
Gelly Ristor
  • 573
  • 1
  • 6
  • 17
19
votes
2 answers

change thread name on linux (htop)

I have a multithread application and I would like that htop (as example) shows a different name per each thread running, at the moment what it shows is the "command line" used to run the main. I have tried using prctl(PR_SET_NAME, .....) but it…
Gaetano Mendola
  • 1,344
  • 3
  • 12
  • 27
19
votes
3 answers

Check to see if a pthread mutex is locked or unlocked (After a thread has locked itself)

I need to see if a mutex is locked or unlocked in an if statement so I check it like this... if(mutex[id] != 2){ /* do stuff */ } but when I check it gcc gives me the following error: error: invalid operands to binary != (have 'ptherad_mutex_t'…
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
19
votes
3 answers

What are the POSIX cancellation points?

What are the POSIX cancellation points? I'm looking for a definitive list of POSIX cancellation points. I'm asking because I have a book that says accept() and select() are cancellation points, but I've seen sites on the internet claim that they are…
Corin Fletcher
  • 1,611
  • 1
  • 17
  • 25