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

what does it mean by thread serialization in c++?

I know about serializing objects and how they are saved to disk, but what does thread serialization actually mean? Could any one help me on this one and point me in the right direction please?
pokche
  • 1,141
  • 12
  • 36
16
votes
1 answer

Is libuv thread safe?

I have created a new thread dedicated to a libuv run loop. The thread function looks something like this: void thread_function() { uv_loop_t *loop = uv_loop_new(); uv_ref( loop ); uv_run( loop ); } The ref counter increment keeps the thread…
user1157123
16
votes
2 answers

Race condition in glibc/NPTL/Linux robust mutexes?

In a comment on the question Automatically release mutex on crashes in Unix back in 2010, jilles claimed: glibc's robust mutexes are so fast because glibc takes dangerous shortcuts. There is no guarantee that the mutex still exists when the kernel…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
15
votes
6 answers

What does mutex and semaphore actually do?

I want some clarification regarding mutex and semaphore. My question is, What mutex actually do when a thread tries to enter a region locked by a mutex, a. it waits for the lock to be released? or b. it goes to sleep until the lock is…
P basak
  • 4,874
  • 11
  • 40
  • 63
15
votes
9 answers

Does order of unlocking mutexes make a difference here?

Let's say I have two variables, protected_var1 and protected_var2. Let's further assume that these variables are updated via multiple threads, and are fairly independent in that usually one or the other but not both is worked on - so they both have…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
15
votes
4 answers

Is it safe to call pthread_cancel() on terminated thread?

I'm wondering if it is safe to call pthread_cancel() on a terminated thread. I couldn't find any hints in the manual page. Thanks in advance for any hints. Edit: Maybe I wasn't accurate enough. I'm not talking about threads terminated by an earlier…
domachine
  • 1,119
  • 1
  • 12
  • 20
15
votes
2 answers

How to make pthread_cond_timedwait() robust against system clock manipulations?

Consider the following source code, which is fully POSIX compliant: #include #include #include #include #include #include int main (int argc, char ** argv) { pthread_cond_t c; …
Mecki
  • 125,244
  • 33
  • 244
  • 253
15
votes
7 answers

pthread - How to start running a new thread without calling join?

I want to start a new thread from the main thread. I can't use join since I don't want to wait for the thread to exit and than resume execution. Basically what I need is something like pthread_start(...), can't find it though. Edit: As all of the…
Ohad Horesh
  • 4,340
  • 6
  • 28
  • 45
15
votes
4 answers

how to pass data to running thread

When using pthread, I can pass data at thread creation time. What is the proper way of passing new data to an already running thread? I'm considering making a global variable and make my thread read from that. Thanks
monkeyking
  • 6,670
  • 24
  • 61
  • 81
15
votes
5 answers

Is PThread a good choice for multi-platorm C/C++ multi-threading program?

Been doing mostly Java and smattering of .NET for last five years and haven't written any significant C or C++ during that time. So have been away from that scene for a while. If I want to write a C or C++ program today that does some…
RogerV
  • 3,826
  • 4
  • 28
  • 32
15
votes
2 answers

Using pthread mutex shared between processes correctly

There are many questions on stackoverflow about if a pthread mutex can be shared between processes, but I found no questions/answers regarding initialization of the shared mutex. As far as I understand, the common way of using a process-shared mutex…
Rom098
  • 2,445
  • 4
  • 35
  • 52
15
votes
5 answers

What happens when a thread forks?

I know calling fork() sys_call from a thread is a bad idea. However, what will happen if a thread creates a new process using fork()? The new process will be the child of the main thread that created the thread. I think. If its parent finishes…
Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
15
votes
6 answers

How to calculate number of processor cores in PHP script (linux)?

I'm trying to use pthreads for multithreading. I'm creating pool with constructor. First parameter is number of Workers. $pool = new Pool(8, 'WebWorker'); I want to detect count of processor cores automatically. Something like this: $pool = new…
Nick
  • 9,735
  • 7
  • 59
  • 89
15
votes
2 answers

CMake failing to detect pthreads due to warnings

I get an error when making a project with CMake: -- Could NOT find Threads (missing: Threads_FOUND) The error log shows that CMake tripped up over something truly banal: /usr/bin/cc -std=c11 -D_GNU_SOURCE -Wall -Wextra -Wpointer-arith -Wundef…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
15
votes
2 answers

What is g++'s -pthread equiv in clang?

I'm switching over from g++ to clang however, in g++, I have the -pthread flag, which clang does not seem to recognize. What is the equiv in clang? EDIT: My clang build is pulling from svn on March 5 2010.
anon
  • 41,035
  • 53
  • 197
  • 293