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

gcc undefined reference to `pthread_atfork'

I'm trying to compile a recent version of openssl on an optware-ng install with gcc 7.2.0 on x86_64 architecture The system has 2 libpthreads, one in /lib:/usr/lib:/lib64:/usr/lib64 (link to each other). And one in the optware-ng install in…
Koen
  • 81
  • 3
3
votes
1 answer

Replacement for Windows specific HANDLE, Event Creation and Sync API in case of Linux

I have a following set of codes specific to Windows, //1: Declaring HANDLE HANDLE *m_handle; //2: Creating HANDLE instance int m_Count = 4; m_handle = new HANDLE[m_Count]; //3: Creating Events for (int i = 0; i <…
Sharath Kumar
  • 73
  • 1
  • 9
3
votes
0 answers

Can't run simple Emscripten thread example in Firefox

I'd like to compile the following C++ snippet using threads with Emscripten #include #include void foo() { puts("foo\n"); } void bar(int x) { printf("bar %d\n", x); } int main(int argc, char*…
Vinci
  • 1,382
  • 10
  • 12
3
votes
2 answers

thread terminate called without an active exception

I've been doing threaded networking for a game, but the server dies randomly, while i've been testing the networking so that I have several clients connecting and sending bunch of packets and disconnecting then connecting back again. I am using c++…
Tonto
  • 324
  • 1
  • 3
  • 13
3
votes
1 answer

Passing std::promise object to a function via direct function call

I am learning the std::promise and std::future in C++. I wrote one simple program to calculate the multiplication of two numbers. void product(std::promise intPromise, int a, int b) { intPromise.set_value(a * b); } int main() { int a =…
goodman
  • 424
  • 8
  • 24
3
votes
3 answers

Does pthread_once_t variable need a mutex?

we use One-Time initialization for pthreads like this: /* define a statically initialized pthread_once_t variable */ pthread_once_t once_var = PTHREAD_ONCE_INIT; /* we call pthread_once function in threads */ int pthread_once(pthread_once_t…
Majid Azimi
  • 5,575
  • 13
  • 64
  • 113
3
votes
3 answers

Threaded server, sending messages between clients

I'm writing a C network server that will pair two clients together and allow them to send messages to each other. At the moment, each client has their own thread in the server and in the thread I have a loop that is basically while((numBytesRead =…
JimR
  • 2,145
  • 8
  • 28
  • 37
3
votes
3 answers

How to properly remove Mutex?

Using C++, in one of my destructors, i say mutex = NULL; This however results in an error "No viable overloaded '='" in my Xcode. Same mutex was previously initialized in a constructor as mutex = PTHREAD_MUTEX_INITIALIZER; Please advise, how can i…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
3
votes
1 answer

How to get a pthread name in C

Say I create a pthread as pthread_t lift_3; and pthread_create(&lift_1, NULL, lift, share);. When it goes into lift(), how can I get it the function to print the actual name of the thread? Or set a name for the thread? I have tried using…
Zam-Oxy
  • 130
  • 10
3
votes
6 answers

Multithreaded spin lock?

My daemon initializes itself in four different threads before it starts doing its things. Right now I use a counter which is incremented when a thread is started and decremented when it is finished. When the counter hits 0 I call the initialization…
Kristina
  • 15,859
  • 29
  • 111
  • 181
3
votes
2 answers

pthreads: perform function on main thread

I'm looking for the analogous of Cocoa's -[NSObject performSelectorOnMainThread: withObject: waitUntilDone:] method. So basically I have a function that does some work on a separate thread but it must perform some synchronous calls that need to be…
Ivan
3
votes
7 answers

What can make a program run slower when using more threads?

This question is about the same program I previously asked about. To recap, I have a program with a loop structure like this: for (int i1 = 0; i1 < N; i1++) for (int i2 = 0; i2 < N; i2++) for (int i3 = 0; i3 < N; i3++) for (int i4 = 0;…
David Z
  • 128,184
  • 27
  • 255
  • 279
3
votes
0 answers

Stack overflow when adding link dependency. Available stack reduced with linking?

Problem I get a stack overflow if I link against a library, and I don't get it if I don't. Here is the code: #include #include #include #include #define ASSERT_FATAL(cond) \ do { if (!(cond)) {…
cjb
  • 59
  • 3
3
votes
2 answers

Signal handler behavior in multi threaded environment

I have the following program where only one thread installs the signal handler. But when I tested the code by sending signal to each thread, all the threads execute the signal handler. Does all the threads share the same signal handler. I was under…
Karthick
  • 1,010
  • 1
  • 8
  • 24
3
votes
2 answers

How do I read/write a shared variable with pthreads?

I have two threads, using C pthreads on linux. One of them writes data and the other is reading it. I'm using a variable to allow the read thread when is allowed to read and the write one when is allowed. So the mutex applies to this boolean…
alvatar
  • 3,340
  • 6
  • 38
  • 50