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
23
votes
3 answers

is nice() used to change the thread priority or the process priority?

The man page for nice says "nice() adds inc to the nice value for the calling process. So, can we use it to change the nice value for a thread created by pthread_create? EDIT: It seems that we can set the nice value per thread. I wrote an…
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
23
votes
4 answers

libc source location - for download or online viewing?

Sorry I know this is stupid but where is linux libc source code available? What I downloaded from GNU didn't seem to be what I wanted, specifically I could find nothing in the pthreads function family. Is there an online (hypertexted…
ValenceElectron
  • 2,678
  • 6
  • 26
  • 27
23
votes
2 answers

Thread name longer than 15 chars?

By using functions like prctl, or pthread_set_name_np it's possible to change the name of a thread. The limit both functions imposes, at least in Linux 2.6.38, is that the name cannot be longer than 15 characters (NULL termination being the 16th…
dsvensson
  • 1,401
  • 12
  • 20
23
votes
3 answers

How does pthread work?

I am experienced at multithreaded programming in Java and C#, and am starting to learn how to do it in C on Linux. I "grew up" in the programming sense on Linux, so I understand it's memory philophy, process handling, etc. at a high level. My…
Michael K
  • 3,297
  • 3
  • 25
  • 37
23
votes
5 answers

How to create an efficient multi-threaded task scheduler in C++?

I'd like to create a very efficient task scheduler system in C++. The basic idea is this: class Task { public: virtual void run() = 0; }; class Scheduler { public: void add(Task &task, double delayToRun); }; Behind…
geza
  • 28,403
  • 6
  • 61
  • 135
23
votes
2 answers

pthread synchronized blocking queue

I'm looking for a recommended implementation of a thread-safe blocking queue (multi producer/consumer) in C using pthread synchronization semantics.
Mike
  • 58,961
  • 76
  • 175
  • 221
23
votes
5 answers

Is fork (supposed to be) safe from signal handlers in a threaded program?

I'm really uncertain about the requirements POSIX places on the safety of fork in the presence of threads and signals. fork is listed as one of the async-signal-safe functions, but if there is a possibility that library code has registered…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
23
votes
5 answers

link to pthread library using CMake (in CLion)

I've looked all over and I can't figure out how to get CLion to link the lpthread library. I know that w/ gcc you can just type -lpthread, but I need to do some debugging in CLion. Here's my current CMakeLists file: cmake_minimum_required(VERSION…
dcgenjin
  • 1,108
  • 3
  • 12
  • 25
23
votes
3 answers

What happens if you call exit(0) while other threads are still running?

Suppose a program has several threads: t1, t2, etc. These are using pthreads. The t2 thread is sitting in a loop reading from a stream and accessing a variable with static storage duration. Now suppose t1 calls exit(0). (Further details: I have a…
mbells
  • 3,668
  • 3
  • 22
  • 21
23
votes
2 answers

How to use pthread_atfork() and pthread_once() to reinitialize mutexes in child processes

We have a C++ shared library that uses ZeroC's Ice library for RPC and unless we shut down Ice's runtime, we've observed child processes hanging on random mutexes. The Ice runtime starts threads, has many internal mutexes and keeps open file…
Blair Zajac
  • 4,555
  • 3
  • 25
  • 36
23
votes
5 answers

PThread vs boost::thread?

Having no experience with threading in the past, which threading technique in C++ will be the easiest for a beginner? boost::thread or pthreads?
Chris
  • 21,549
  • 25
  • 71
  • 99
22
votes
4 answers

Threads and file descriptors

Do different threads within a single process have distinct independent file descriptor tables? If multiple threads within the same process concurrently access a single file, will the offset into the file for two different calls to open performed by…
Lipika Deka
  • 3,774
  • 6
  • 43
  • 56
22
votes
2 answers

pthread_cancel() alternatives in Android NDK?

I am porting a mid-sized body of C++ code to Android NDK. Unfortunately the pthreads implementation (as of NDK v5, anyway) is incomplete. Specifically, our application relies on pthread_cancel() to kill a worker thread. NDK does not implement…
Chuck Fry
  • 387
  • 1
  • 2
  • 10
22
votes
1 answer

Timespec redefinition error

While executing a Pthread program in C using Visual Studio 2015, I got the following error: Error C2011 'timespec': 'struct' type redefinition The following is my code: #include #include #include void…
Vijay Manohar
  • 473
  • 1
  • 7
  • 22
22
votes
2 answers

Unix pthreads and signals: per thread signal handlers

I'm having trouble getting threads to catch the correct signals. For example, I first start a main thread (tid 1). Then, it sets a signal handler for SIGUSR1 to function1(), using signal(2). The main thread creates a new thread, with tid 2. In…
tomKPZ
  • 827
  • 1
  • 10
  • 17