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

ps display thread name

Is there a way for ps (or similar tool) to display the pthread's name? I wrote the following simple program: // th_name.c #include #include void * f1() { printf("f1 : Starting sleep\n"); sleep(30); printf("f1 :…
Ahmed A
  • 3,362
  • 7
  • 39
  • 57
16
votes
5 answers

Best way to start a thread as a member of a C++ class?

I'm wondering the best way to start a pthread that is a member of a C++ class? My own approach follows as an answer...
jdt141
  • 4,993
  • 6
  • 35
  • 36
16
votes
1 answer

How to use pthreads with Android NDK?

As I know pthreads is a part of C library. Android has Bionic C library. How to use pthreads with Android NDK?
Evgeny Vinnik
  • 1,235
  • 2
  • 16
  • 27
16
votes
4 answers

Is accept() thread-safe?

I'm currently writing a simple webserver in C for a course I'm doing. One requirement is for us to implement a thread pool to handle connections using pthreads. I know how I would go about doing this roughly(calling accept in a main thread and…
CalumMcCall
  • 1,665
  • 4
  • 24
  • 46
16
votes
3 answers

g++ - Why do I have to pass "-pthread" option while using std::thread?

I'm just trying to understand a concept used by g++. Here my very simple std::thread application: #include #include void func() { std::cout << "Running..." << std::endl; } int main() { std::thread t(func); …
user5255670
16
votes
4 answers

Tutorial on Using OpenSSL with pthreads

OpenSSL documents state that it can safely be used in multi-threaded applications provided that at least two callback functions are set, locking_function and threadid_func.... I've written programs which use OpenSSL API. Moreover, I know how to use…
Sadeq Dousti
  • 3,346
  • 6
  • 35
  • 53
16
votes
2 answers

OSX - How can I see the TID of all threads from my process?

On Linux ps -eLf | grep my-process-name gives a list of the threads within my process along with the TID of each thread. On OSX ps -M pid gives me the list of the threads but does not show the TID of each thread. How can I see thread TIDs under a…
user4417144
16
votes
2 answers

Does pthread_mutex_lock contains memory fence instruction?

Do pthread_mutex_lock and pthread_mutex_unlock functions call memory fence/barrier instructions? Or do the the lower level instructions like compare_and_swap implicity have memory barriers?
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
16
votes
1 answer

Error message "undefined reference for `CPU_ZERO'"

I included: #include #define _GNU_SOURCE Then in my code I have written (brief mention): cpu_set_t set; CPU_ZERO(&set); CPU_SET(proc_num, &set); if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &set)) { …
muskaan
  • 177
  • 2
  • 2
  • 11
16
votes
6 answers

PHP pthreads: Fatal error: Class 'Thread' not found

I use php5.5 on my webserver. Now I want to use pthreads. Here's my php config: http://dd19010.kasserver.com/infophp.php55 After implementing this code.....
ItsMeDom
  • 540
  • 3
  • 5
  • 18
16
votes
2 answers

pthread functions "_np" suffix

What does "_np" suffix mean here: pthread_mutex_timedlock_np or in macros PTHREAD_MUTEX_TIMED_NP Upd: From glibc2.2 enum { PTHREAD_MUTEX_TIMED_NP, PTHREAD_MUTEX_RECURSIVE_NP, PTHREAD_MUTEX_ERRORCHECK_NP, PTHREAD_MUTEX_ADAPTIVE_NP…
osgx
  • 90,338
  • 53
  • 357
  • 513
16
votes
5 answers

How can you find the processor number a thread is running on?

I have a memory heap manager which partitions the heap into different segments based on the number of processors on the system. Memory can only be allocated on the partition that goes with the currently running thread's processor. This will help…
Patrick Niedzielski
  • 1,194
  • 1
  • 8
  • 21
16
votes
5 answers

pthreads : pthread_cond_signal() from within critical section

I have the following piece of code in thread A, which blocks using pthread_cond_wait() pthread_mutex_lock(&my_lock); if ( false == testCondition ) pthread_cond_wait(&my_wait,&my_lock); pthread_mutex_unlock(&my_lock); I have the…
curryage
  • 161
  • 1
  • 1
  • 4
16
votes
3 answers

How can you get the Linux thread Id of a std::thread()

I was playing with std::thread and I was wondering how is it possible to get the thread id of a new std::thread(), I am not talking about std::thread::id but rather the OS Id given to the thread ( you can view it using pstree). This is only for my…
Char Aznable
  • 165
  • 1
  • 1
  • 5
16
votes
3 answers

How do I create a global variable that is thread-specific in C using POSIX threads?

I am learning about POSIX threads and I have come to the section on Thread Specific Data. The book does an excellent example using a file descriptor. However, I wanted to do the same example on my own, except this time using a global variable.…
Cal
  • 734
  • 1
  • 9
  • 25