8,998 questions
2
votes
2
answers
74
views
Race condition between signal and pthread_cond_wait
Given
volatile int stopping = 0;
void handler(int) {
stopping = 1;
}
int main(int, char **) {
pthread_sigmask(BLOCK, [INT], ...);
pthread_create();
pthread_sigmask(UNBLOCK, [INT], ......
4
votes
1
answer
135
views
Why does thread 1 not exit?
I have a more complex situation that uses pthreads, synchronized with condition variables. Each thread had its own condition variable so there is no access problem.
The code runs a loop of 10 ...
0
votes
1
answer
116
views
Deferred cancellation in C [duplicate]
I am learning on how threads can be cancelled. Asynchronous cancellation can cancel the thread at any given point in time, but what I have learned about deferred cancellation is that it can only be ...
1
vote
0
answers
108
views
std::shared_mutex priority inversion
pthread_rwlock_wrlock are used inside real-time threads to cause hang to live, Why would you live?
There is a priority inversion bug described here involving pthread_rwlock_wrlock and real time ...
0
votes
1
answer
133
views
strange pthread and posix signal application behaviour
I'm having troubles understanding the behaviour of the following application.
There are 3 threads:
worker thread with an infinite loop which locks the mutex, modifies a global variable but in the end ...
0
votes
1
answer
60
views
Is it guaranteed that the thread_id from pthread_threadid_np is never 0 on macOS?
It seems to be true but I cannot find the docs that explicitly says that. The man page says
The pthread_threadid_np() function stores the system-wide unique
integral
ID of thread in the location ...
1
vote
1
answer
111
views
Why pthread_rdlock doesn`t downgrade the lock?
Code:
pthread_rwlock_wrlock(&memlock); // (1)
// do something...
pthread_rwlock_rdlock(&memlock); // (2)
// do something...
pthread_rwlock_wrlock(&memlock); // (3)
The following tags, (1),...
3
votes
2
answers
160
views
Guaranteed-invalid value for pthread_t?
PURPOSE: I want to create a thread pool: essentially an array of pthread_t values. I want to be able to terminate threads in progress, and to mark their pthread_t value as invalid, thus the slot is ...
3
votes
1
answer
100
views
munmap shared memory with locked robust mutex makes it deadlock, why?
I need to use robust mutexes for IPC in case one of my processes crashes while holding a mutex locked. Definition is clear from e.g. man pthread_mutexattr_setrobust and I will not repeat it here.
I ...
2
votes
1
answer
106
views
`pthread_kill` randomly send signal to the wrong thread on MacOS
I have multiple threads performing sigwait on the same signal, and the main thread will wake specific thread up via pthread_kill. On Linux the program runs fine, but on MacOS the wrong thread will be ...
5
votes
2
answers
279
views
Signal handling in multi-threaded scenario [closed]
I'm trying to revive Hemlock, which now appears abandoned (original author has been absent for some time now). Mostly this has been a straightforward process of clearing out the bit rot, but I'm now ...
0
votes
1
answer
128
views
dispatch_main and main thread stack [closed]
We're seeing some strange crashes and noticed the following. It's unclear if related or not.
The contract for xpc_main, which internally calls dispatch_main, is This function never returns. and they ...
0
votes
1
answer
74
views
when is `-pthread` necessary? [duplicate]
I am trying to understand when -pthread is needed. For code that directly uses pthread API, of couse -pthread should be passed, but:
should I pass -pthread if my code does not use pthread directly, ...
2
votes
1
answer
95
views
Why does modifying a pthread_attr_t from pthread_getattr_default_np() affect the default attributes?
I'm observing unexpected behavior when using pthread_getattr_default_np() in conjunction with pthread_attr_setaffinity_np() on Linux.
I override the default thread attributes using ...
4
votes
1
answer
111
views
When exactly does a robust POSIX mutex that returns EOWNERDEAD get locked?
I know that, for POSIX shared and robust mutexes, if the process holding them crashes, the next process trying to acquire the lock will successfully call pthread_mutex_lock, which will return ...
5
votes
0
answers
127
views
`pthread_exit(0)` raising SIGABRT when called from a method marked as `noexcept`
Summary:
I have encountered some behaviour I don't understand when using pthreads with C++17. When calling pthread_exit() from a method marked as noexcept, SIGABRT is raised. While I have some ideas ...
2
votes
1
answer
86
views
Having different result from the serial and concurrent versions of the same program [closed]
I am in a university course focused on parallel and concurrent programming. My professor left us some homework to do, and three of them are basically the same project. My first revision of the project ...
2
votes
1
answer
109
views
pthread_rwlock_wrlock after pthread_rwlock_rdlock does not return EDEADLK
In the documentation at Open Group, it states that:
NAME
pthread_rwlock_wrlock, pthread_rwlock_trywrlock - lock a read-write lock object for writing
...
The pthread_rwlock_wrlock() and ...
1
vote
0
answers
40
views
Why is pthread_setname_np and PR_SET_NAME limited to 16 bytes? [duplicate]
Why is pthread_setname_np and PR_SET_NAME limited to 16 bytes?
Linux processes can have much longer names, for example systemd-timesyncd comes pre-installed on my system, and is 18 bytes including the ...
1
vote
2
answers
122
views
Leak errors in fifo application in c
I'm not sure why my valgrind is spitting heap errors and I've been tracing my code left and right. I have some code, but I'm not sure if more code is needed.
My jobs.h
#include "piper.h"
#...
1
vote
1
answer
90
views
Can pthread_barrier_t be used across multiple processes with shared memory?
I'm working on a C++ application that launches multiple processes. All of them load the same executable and share a memory region using shm_open + mmap.
In that shared memory, we store a ...
1
vote
0
answers
56
views
thread cancellation points in Rust [duplicate]
Does Rust threads have concept of thread cancellation points just like POSIX threads. In POSIX, a cancellation request can be sent to a thread using pthread_cancel function. Also, thread's ...
3
votes
0
answers
83
views
How to Properly Exit Main Process from a Thread in C? [closed]
I am working on a C program that simulates a hair salon, and I'm facing an issue with terminating the main process from the simulation_timer_thread. The only way I can successfully exit the main ...
6
votes
2
answers
155
views
How to terminate early while waiting for multiple threads using pthread?
Lets say I spawn 4 threads, running these functions
int func1() {sleep(2); return 1};
int func2() {sleep(4); return 0};
int func3() {sleep(6); return 2};
int func4() {sleep(8); return 3};
I want to ...
4
votes
2
answers
109
views
C Multithreading - optimizing return handling
So, I am currently writing a program that uses the standard pthread.h library.
I want there to be multiple threads, all calculating stuff independently of each other at the same time (no mutexes ...
1
vote
1
answer
147
views
Why is my program with semaphore not achieving mutual exclusion in a multi-threaded increment operation?
I am trying to implement a simple multi-threaded program where two threads increment a shared counter (cnt) n times. I want to use a semaphore to ensure mutual exclusion and prevent race conditions. ...
0
votes
1
answer
194
views
Where does the try_compile output go?
Clarification: When I say "output" here, I'm not talking about the .o or .exe or whatever. I'm talking about the warnings and errors emitted during my failed attempt to find the Threads ...
0
votes
1
answer
70
views
Synchronization Thread in Pthread
Suppose I'm creating 2 thread and I'm also created synchronization.
Thread 1 : program to adding 2 integer.
Thread 2 : program to subtract 2 integer
In multithread thread 1 and thread 2 will
be ...
2
votes
1
answer
87
views
TCP clients not receiving messages in multi-threaded C application
I am learning multi-threading in C and I'm trying to build a simple chat application. The idea is that clients connect to a central server and the server forwards messages sent by the clients to the ...
0
votes
0
answers
114
views
How can I safely perform context switching on an OS thread preemptively (not cooperatively) with near to no performance loss
TLDR; towards the bottom
I am trying to build a runtime with Green Threads for my language which are inspired by Golang's go routines and scheduler.
The runtime is written in C++ and I am trying to ...
1
vote
1
answer
350
views
ThreadSanitizer reports a data race despite mutex-protected access in pthread_cancel cleanup handler
I am encountering a puzzling issue with ThreadSanitizer while using pthread_cancel and a cleanup handler in a multithreaded C++ program. The sanitizer reports a data race on a global variable even ...
1
vote
1
answer
68
views
what is the real memory usage of pthread_create
I wrote a simple program of
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *thread(void *arg) {
printf("thread() entered with argument '%s'\n", arg);
...
-3
votes
2
answers
150
views
What is the best way to make a parent thread wait after initializing a child pthread until it receives some signal from the child?
Immediately after spawning a thread with pthread_create, I would like the parent to wait an arbitrary amount of time until the child thread allows it to continue. Here is how I might approach it with ...
0
votes
1
answer
50
views
Issue with Thread Synchronization Using Semaphores (Printing Sequence)
Question:
I'm working on a C program where I have two threads, p1 and p2, and I need them to print the sequence 1234 1234 1234 .... I am using semaphores to synchronize the threads, but the output is ...
1
vote
2
answers
148
views
Detaching a thread versus calling pthread_exit() from main(), consequences for resources and memory in both scenarios
I'm fairly new to C programming and am at the moment trying to wrap my head around the pthreads library and threading more generally.
Question 1:
When and why is detaching a thread a nice option to ...
1
vote
1
answer
194
views
Relation of sigprocmask and pthread_sigmask for multithreaded program
sigprocmask(2) says:
Each of the threads in a process has its own signal mask.
, but also:
The use of sigprocmask() is unspecified in a multithreaded process; see pthread_sigmask(3).
...
2
votes
0
answers
128
views
Need help to multi-thread in my Minecraft clone
I am currently creating a Minecraft clone in C and OpenGL. I have implemented a chunk loading system however this would result in freezing as new chunks are loaded as the main thread is having to wait ...
0
votes
0
answers
68
views
clock_nanosleep is giving out inconsistent latency during wakeup in rt system along with some spike of upto 1ms
I am new to rt c++ development and sorry if i am missing out on something basic.
I'm experiencing inconsistent latency behavior in a real-time system using clock_nanosleep for precise wakeups. The ...
1
vote
0
answers
58
views
pthread_cond_broadcast not waking up threads waiting with pthread_cond_wait [duplicate]
I'm just facing a problem with my threads. I'm coding a program that needs to execute asynchronous tasks, and I do it via a thread pool.
To avoid my threads running in the void when they have nothing ...
1
vote
0
answers
85
views
About threads and semaphore (POSIX semaphores)
I want every time a student is created to display who has been created and in which department they study. I'm stuck and I don't know what parameters to pass to printInfo(), do you have any idea? I ...
1
vote
1
answer
130
views
Thread synchronization with semaphore in C
I am working on an assignment which requires me to simulate a bus travelling from Stop A to Stop B over and over again while students can get on and get out of it.
I coded the whole routine of the bus ...
0
votes
1
answer
185
views
Is it reliable to use pthread_self() to get a thread ID even from C++ threads?
On Linux, libstdc++ doesn't use pthread_create() to create threads, as it can be seen from this bug: https://bugzilla.kernel.org/show_bug.cgi?id=218607 (libpsx wraps pthread_create() to intercept ...
0
votes
0
answers
62
views
Error in Visual Studio when trying to use threads using pthreas
I get a C2011 error each time i try executing despite having the library installed. This was the video I follow along for install.
#include <stdio.h>
#include <pthread.h>
void* PrintHello(...
0
votes
1
answer
108
views
How does printf() works as a safe cancel point? What is it depends on?
I have this little program that is supposed to create a thread and cancel it.
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *...
0
votes
0
answers
93
views
Pthread (or threads) wait for any one thread to finish
I am using C++. I have a set of files to be processed by another program (Executable). I intend to run this process in multiple threads (not more than N threads to prevent system from being becoming ...
2
votes
0
answers
75
views
Am I understanding memory ordering models correctly? (cont'd)
This is a follow-up of this question. I have updated the code to incorporate some of the ideas pointed out by Peter Cordes.
My threads should be doing actual parallel work (now it takes time to the ...
2
votes
1
answer
131
views
Am I understanding memory ordering models correctly?
I am learning about C memory ordering models, and I came up with this little code for a producer and consumer sharing a "bucket" with "units".
I intended to create this sequence of ...
0
votes
1
answer
110
views
Unexpected behavior with pthread read-write lock in a multiprocess multithreaded shared memory environment
I'm working in a multiprocess, multithreaded environment where I'm using a pthread_rwlock_t read-write lock stored in shared memory so that all processes can access it. Here's how I initialized the ...
1
vote
0
answers
85
views
How to debug an exception in a multithread server class
I have a socket class that has a separate thread for recv/send data, and it is able to send/receive data correctly.
The part of the logic that is not working and giving me an exception is the part ...
0
votes
0
answers
70
views
Trouble writing and reading from files using threads
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <...