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

A very simple thread pool using pthreads in C++

I'm trying to understand some of the basics of using POSIX pthreads. The kind of thing I need to do (eventually) is parallelize some computations, using a thread pool model. At present I want to ensure I have a very basic sense for how the POSIX…
Ryan Budney
  • 308
  • 1
  • 6
  • 16
14
votes
9 answers

pthreads in C - pthread_exit

For some reason I thought that calling pthread_exit(NULL) at the end of a main function would guarantee that all running threads (at least created in the main function) would finish running before main could exit. However when I run this code below…
Siggi
  • 141
  • 1
  • 1
  • 3
14
votes
6 answers

How to allow certain threads to have priority in locking a mutex use PTHREADS

Assume that the following code is being executed by 10 threads. pthread_mutex_lock(&lock) Some trivial code pthread_mutex_unlock(&lock) For purpose of explanations lets say the threads are T1, T2, T3.....T10. My requirement is that as long as T1 or…
ghayalcoder
  • 1,675
  • 2
  • 17
  • 24
14
votes
3 answers

What time function do I need to use with pthread_cond_timedwait?

The pthread_cond_timedwait function needs an absolute time in a time timespec structure. What time function I'm suppose to use to obtain the absolute time. I saw a lot of example on the web and I found almost all time function used. (ftime, clock,…
Vincent
  • 2,712
  • 5
  • 24
  • 27
14
votes
4 answers

Multi-threaded C program much slower in OS X than Linux

I wrote this for an OS class assignment that I've already completed and handed in. I posted this question yesterday, but due to "Academic Honesty" regulations I took it off until after the submission deadline. The object was to learn how to use…
Alex Popov
  • 2,509
  • 1
  • 19
  • 30
14
votes
3 answers

How is pthread_join implemented?

I'm a little new to threading, so you'll have to forgive the naiveté of this question. How is pthread_join implemented and how does it effect thread scheduling? I always pictured pthread_join implemented with a while loop, simply causing the…
Tom
  • 18,685
  • 15
  • 71
  • 81
14
votes
3 answers

Threading issues

getvariana: tpp.c:63: __pthread_tpp_change_priority: Assertion `new_prio == -1 || (new_prio >= __sched_fifo_min_prio && new_prio <= __sched_fifo_max_prio)' failed. Hi all, I am trying to re-run a program which creates 5 threads and after…
Adit Ya
  • 731
  • 1
  • 8
  • 19
14
votes
5 answers

Detached pthreads and memory leak

Can somebody please explain to me why this simple code leaks memory? I believe that since pthreads are created with detached state their resources should be released inmediatly after it's termination, but it's not the case. My environment is…
Fracu
  • 835
  • 1
  • 13
  • 28
14
votes
3 answers

How to cleanly interrupt a thread blocking on a recv call?

I have a multithreaded server written in C, with each client thread looking something like this: ssize_t n; struct request request; // Main loop: receive requests from the client and send responses. while(running && (n = recv(sockfd, &request,…
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
14
votes
7 answers

Porting windows manual-reset event to Linux?

Is there any easier solution in porting a windows manual-reset event to pthread, than a pthread conditional-variable + pthread mutex + a flag if event is set or unset?
sfhdhsf
14
votes
1 answer

How to use thread-sanitizer of gcc v4.8.1?

gcc v4.8.x add options for debugging your program: -fsanitize=thread Enable ThreadSanitizer, a fast data race detector. Memory access instructions will be instrumented to detect data race bugs. See…
husthl
  • 487
  • 1
  • 3
  • 11
14
votes
1 answer

C pthread synchronize function

Is there a function in pthread library to synchronize threads? Not mutexes, not semaphores, just one call function. It is supposed to lock the threads that get in that point until all the threads reach such function. E.g: function thread_worker(){ …
Frederico Schardong
  • 1,946
  • 6
  • 38
  • 62
14
votes
2 answers

Linux - threads and process scheduling priorities

if we create pthreads (pthread_create) or processes (fork) with default scheduling policies on linux, will the scheduler treats the processes and threads with same priority while scheduling them? let us say there is process P1 with one thread and…
Medicine
  • 1,923
  • 2
  • 23
  • 33
14
votes
3 answers

In g++ is C++ 11 thread model using pthreads in the background?

I am just trying my hands on g++ 4.6 and C++11 features. Every time I compile a simple threading code using -std=c++0x flag, either it crashes with segmentation fault or it just throws some weird exception. I read some questions related to C++11…
Recker
  • 1,915
  • 25
  • 55
13
votes
3 answers

What is the Difference B/W TCB(Thread control block) & PCB(Process)

A process control block (PCB) and a Thread Control Block (TCB) are both used in linux kernels to have time on the CPU delegated to them. What are the difference between the two? What information is generally maintained in a process control bloc…
Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54