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

Wait on multiple condition variables on Linux without unnecessary sleeps?

I'm writing a latency sensitive app that in effect wants to wait on multiple condition variables at once. I've read before of several ways to get this functionality on Linux (apparently this is builtin on Windows), but none of them seem suitable for…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
29
votes
6 answers

Why are threads called lightweight processes?

A thread is "lightweight" because most of the overhead has already been accomplished through the creation of its process. I found this in one of the tutorials. Can somebody elaborate what it exactly means?
Vijay
  • 65,327
  • 90
  • 227
  • 319
29
votes
13 answers

How to join a thread that is hanging on blocking IO?

I have a thread running in the background that is reading events from an input device in a blocking fashion, now when I exit the application I want to clean up the thread properly, but I can't just run a pthread_join() because the thread would never…
Grumbel
  • 6,585
  • 6
  • 39
  • 50
28
votes
2 answers

How can I kill a pthread that is in an infinite loop, from outside that loop?

I create a thread and I put it into an infinite loop. I get memory leaks when checking the code with valgrind. Here is my code: #include #include void thread_do(void){ while(1){} } int main(){ pthread_t th; …
Pithikos
  • 18,827
  • 15
  • 113
  • 136
28
votes
7 answers

Forcing a spurious-wake up in Java

This question is not about, whether spurious wakeups actually happen, because this was already discussed in full length here: Do spurious wakeups in Java actually happen? Therefore this is also not about, why I do have to put a loop around my wait…
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
28
votes
2 answers

What is the difference between pthread_self() and gettid()? Which one should I use?

I'm trying to set the CPU affinity of threads on Linux. I'd like to know which one of the following approaches is recommended: Get thread id using pthread_self() Set CPU affinity using pthread_setaffinity_np(....) by passing the thread id as an…
roelf
  • 361
  • 2
  • 4
  • 5
28
votes
5 answers

condition variable - why calling pthread_cond_signal() before calling pthread_cond_wait() is a logical error?

It's written in POSIX threads tutorial https://computing.llnl.gov/tutorials/pthreads/ that it is a logical error. my question is why it is a logical error? In my program i need to use these signals, however i cannot guarantee that there will be a…
Asher Saban
  • 4,673
  • 13
  • 47
  • 60
28
votes
6 answers

pthread sleep linux

I am creating a program with multiple threads using pthreads. Is sleep() causing the process (all the threads) to stop executing or just the thread where I am calling sleep?
Steveng
  • 1,141
  • 2
  • 11
  • 14
28
votes
2 answers

Is it good practice to lock a pthread mutex before destroying it?

class AAA { ... ~AAA() { pthread_mutex_lock( &m_mutex ); pthread_mutex_destroy( &m_mutex ); } } Question> I saw this code somewhere in a project. Is it good practice to do so? Or it is undefined behavior to lock a…
q0987
  • 34,938
  • 69
  • 242
  • 387
28
votes
1 answer

Does each thread have its own stack?

When I create multiple threads from a process, then does each thread have its own stack, or is it that they share the stack of their parent process. What happens when a thread makes a system call? Do threads also maintain their own kernel stack like…
manav m-n
  • 11,136
  • 23
  • 74
  • 97
28
votes
1 answer

undefined reference to `pthread_create'

I have client server code. LinServer.cpp using pthread to continuously listen client. I created make file to compile all togather: all: LinServer LinClient LinServer: g++ LinServer.cpp -o LinServer -pthread LinClient: g++…
user2500861
28
votes
4 answers

Setting thread priority in Linux with Boost

The Boost Libraries don't seem to have a device for setting a thread's priority. Would this be the best code to use on Linux or is there a better method? boost::thread myThread( MyFunction() ); struct sched_param param; param.sched_priority =…
SlickMcRunFast
  • 293
  • 1
  • 3
  • 9
28
votes
5 answers

What happens to other threads when one thread forks()?

In C++ using pthreads, what happens to your other threads if one of your threads calls fork? It appears that the threads do not follow. In my case, I am trying to create a daemon and I use fork() with the parent exiting to deamonize it. However,…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
27
votes
3 answers

signal and unlock order

void WorkHandler::addWork(Work* w){ printf("WorkHandler::insertWork Thread, insertWork locking \n"); lock(); printf("WorkHandler::insertWork Locked, and inserting into queue \n"); m_workQueue.push(w); signal(); unLock(); } I…
user800799
  • 2,883
  • 7
  • 31
  • 36
27
votes
4 answers

Wake up thread blocked on accept() call

Sockets on Linux question I have a worker thread that is blocked on an accept() call. It simply waits for an incoming network connection, handles it, and then returns to listening for the next connection. When it is time for the program to exit,…
selbie
  • 100,020
  • 15
  • 103
  • 173