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

Efficient and fast way for thread argument

What is the most efficient way to create a thread with argument? The argument is a struct, if the struct can not stay on parent thread stack, there are two solutions. With dynamic memory allocation struct Arg{ int x; int y; }; void*…
Squall
  • 4,344
  • 7
  • 37
  • 46
3
votes
3 answers

Handling two threads, one sleeping and one waiting for input

Question: To create a program which takes user input but times out after some seconds (say it's 2 seconds for now). Approach: I created two threads, one to wait for user input (inputThread with tid[0]) and other to sleep for 2 seconds (sleepThread…
Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29
3
votes
1 answer

pthread_create skips or repeats

pthread_create function gets skipped or sometimes called twice. The question I am solving is: Given a global array that contains number from 1 to 100. You are required to make 10 threads and each thread must find the sum of square of 10…
3
votes
1 answer

cannot convert ‘std::function’ to ‘void* (*)(void*)’

I'm new to using the library, so bear with me. The following code is failing to compile, with this error: error: cannot convert std::function to void* (*)(void*) for argument 3 to int pthread_create(pthread_t*, const…
greg
  • 33
  • 1
  • 5
3
votes
1 answer

Execution order of the code

sample output Can anybody explain the execution order of this code? I'm stuck on the pthread_join part while trying to understand how the code flow works: #include #include #define NUM_THREADS 25 int thread_routine (int x) { …
3
votes
2 answers

pthread execution on linux

I started doing pthread programming on linux and at the very first programme i got totally confused. Below is the programme I am running #include #include #include void *print_message_function( void *ptr ); int…
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
3
votes
2 answers

Dedicated thread (one thread per connection) with buffering capability (c/c++)

My process reads from a single queue tasks that need to be sent to several destinations. We need to maintain order between the tasks (ie task that arrived in the queue at 00:00 needs to be sent before the task that arrived at 00:01) therefore we…
cateof
  • 6,608
  • 25
  • 79
  • 153
3
votes
3 answers

what's the differences between pthread and -pthread options in cmake?

Environment ubuntu 16.04, gcc 5.4.0, cmake 3.5.1 Question target_link_libraries(promise pthread) target_link_libraries(promise -pthread) target_link_libraries(promise -lpthread) What's the differences, which is better…
pingsoli
  • 427
  • 4
  • 11
3
votes
1 answer

overhead of pthread_mutex_lock and pthread_mutex_unlock

I was wondering how much overhead will pthread_mutex_lock and pthread_mutex_unlock cause if it is not a multi-thread context, so I wrote a demo: #include #include #include pthread_mutex_t mutex =…
cong
  • 1,105
  • 1
  • 12
  • 29
3
votes
1 answer

Why some threads don't receive pthread_cond_broadcast?

I have a threadpool of workers. Each worker executes this routine: void* worker(void* args){ ... pthread_mutex_lock(&mtx); while (queue == NULL && stop == 0){ pthread_cond_wait(&cond, &mtx); } el = pop(queue); …
Nymeria
  • 33
  • 5
3
votes
1 answer

Pthreads: The Logic Behind Using While Loops for pthread_cond_wait

I'm having some trouble wrapping my head around why a while loop is used for pthread_cond_wait. Let's take a simple example. Here's some worker thread: pthread_mutex_lock (&loadingLock); while (isReadyToLoad == false){ …
MMMMMCK
  • 307
  • 3
  • 14
3
votes
2 answers

What the Differences between prctl(PR_SET_NAME PR_SET_NAME ) and pthread_setname_np()?

I need to set name for some threads. What are the fifferences between prctl(PR_SET_NAME PR_SET_NAME ) and pthread_setname_np()?
AAA
  • 31
  • 2
3
votes
1 answer

Multithreading concept questions

I just had to write a program in which I have to do matrix multiplication using threads, where there's a thread for every multiplication. Now i'm wondering a few things, Are there really any advantages to using threads for multiplying a 3x2 matrix…
Nano
  • 73
  • 7
3
votes
2 answers

Setting thread's name in another thread on macOS

The function pthread_setname_np takes only one argument on macOS and sets the thread name of the current thread. Basically what it does is call the proc_info syscall as follows (code from the Apple open-source…
Elviss Strazdins
  • 1,404
  • 14
  • 30
3
votes
2 answers

Run a function after returning response

I have an API created with Slim 3. In it I have some curl execution such as sending Push notification to a user. I want send response to requester then execute curl or any other function. I read about Threads in PHP and use pthreads-polyfill but it…