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
1 answer

openMP histogram comparison

I am working on the code that compares image histograms, buy calculating correlation, intersection, ChiSquare and few other methods. General look of these functions are very similar to each other. Usually I working with pthreads, but this time I…
kirbo
  • 1,707
  • 5
  • 26
  • 32
3
votes
1 answer

How many threads can I create inside a C program and how does it relate to the number of threads my CPU has?

My CPU is an i5-8400, which has 6 cores and 6 threads. What does this mean? Initially, I thought it meant that I had 6 threads available per core, which totals 36 threads. Say I'm making a C program, where I create pthreads, does that mean I can…
user12184817
3
votes
3 answers

Cast member function for create_pthread() call

I want to stop the warning server.cpp:823: warning: converting from 'void* (ClientHandler::)()' to 'void ()(void)' in the call: pthread_create(th, NULL, (void* (*)(void*)) &ClientHandler::handle, (void *) clientHandler); where handle() is…
towi
  • 21,587
  • 28
  • 106
  • 187
3
votes
1 answer

For loop not terminating once condition is met using pthread

I was writing a piece of multithreaded code when I found that a for loop was not terminating. The starting code was approximately like this: for(int i = V-1-tid; i >= 0; i-=NTHREADS){ */ stuff */ } V and NTHREADS are constants and tid is the…
zommi
  • 33
  • 4
3
votes
0 answers

Mutexes/Locks in C: C11 `mtx_lock()` vs `pthread_mutex_lock()`

Mutexes were not introduced to the C standard until C11, right? Now that they exist, which should one use or prefer, and when? Why? What are the differences? C11's mtx_lock() vs pthread_mutex_lock(): C11's mtx_lock() From…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
3
votes
2 answers

Implement barrier with pthreads on C

I'm trying to parallelize a merge-sort algorithm. What I'm doing is dividing the input array for each thread, then merging the threads results. The way I'm trying to merge the results is something like this: thread 0 | thread 1…
Artotim
  • 85
  • 5
3
votes
1 answer

What does changing the concurrency level of a thread do?

I was reading the man page of pthread_setconcurrency() and have no idea what it means by "concurrency level". It's simply to vague a term; does changing the concurrency level changes the scheduling policies & process priorities? Does it do any other…
wmjdgla
  • 101
  • 1
  • 8
3
votes
1 answer

Segfault after launching a new thread

I am writing a stock market system that uses several threads to process the incoming orders. The project was going fine until i added one more thread. When i launch the said thread my program segfaults. The segfault is generated in the above thread…
Spyros
  • 67
  • 9
3
votes
2 answers

Stack assignment to a thread

I've been trying to piece together how stack memory is handed out to threads. I haven't been able to piece the whole thing together. I tried to go to the code, but I'm more confused, so I'm asking for your help. I asked this question a little while…
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
3
votes
3 answers

Using mutex locks incorrectly in C++ somehow

I have the following code to try (and fail) to protect a critical region in my code. Basically I want a print statement to be fully executed by any thread before some other thread prints another statement. In main.h: pthread_mutex_t…
ale
  • 11,636
  • 27
  • 92
  • 149
3
votes
2 answers

Shared pthread_cond_broadcast stuck in futex_wait

I have one "server" process a and potentially multiple "client" processes b. The server creates a shared memory file (shm_open) containing a pthread_mutex_t and a pthread_cond_t that it uses for broadcasting to the clients that something has happned…
Poohl
  • 1,932
  • 1
  • 9
  • 22
3
votes
4 answers

Weird thread execution order?

I wrote a small program to see the amount of overhead there is to create a thread. Here is the program (I wrote it quickly so it isn't the best): #include #include void * lala(void * cake) { int * hi = (int *)cake; …
slartibartfast
  • 4,348
  • 5
  • 31
  • 46
3
votes
2 answers

Broadcasting a Signal to All threads in Linux

I want to broadcast a signal from one thread to all other threads in a process. The threads receiving that signal should handle the signal in a signal handler. How can I achieve this? I tried the following code, but it exits by printing User…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
3
votes
0 answers

gdbserver fails in WSL with pthread or anything that links libpthread.so

If I tried to use gdbserver and set any breakpoint anywhere before using first continue in WSL, it fails after starting program with gdbserver: Cannot get thread handle for LWP ###: generic error. If I tried to start program first, gdbserver is…
Tundy
  • 165
  • 13
3
votes
1 answer

Is it possible to print the CPU and core using pthreads on Linux

I'm struggling with getting a multi-threaded app to run on multiple cores. I've looked into affinity, scheduling, etc. Is there a way to find out the CPU Id that any thread is running on? I'm using sched_getaffinity now - but I think that is related…
Jeff
  • 1,969
  • 3
  • 21
  • 35