What causes pthread_mutex_t struct to be all zeros? Core of a deadlocked ARM test application in a gdb session: Answer pthread_mutex_t is used for all mutex types and some of its members are only used for specific mutex types. owner member is set for at least the following mutex types: PTHREAD_MUTEX_ERRORCHEC…
Tag: mutex
How can I accomplish multiple threads to join multiple times into a process? (pthread_mutex_lock)
I want to write a program, what counts to 100. I want to accomplish this with 10 threads, using pthread lock. When the program steps into a thread it generates a number between 0-2, this value will be added to it’s index of array and also to the global variable sum. When the sum value reaches 100, every…
Do we need to unlocking a mutex after recived a signal from cond variable?
I making an app in C for educational purposes with mutexes and conditional variables. Short example here: Can you explain me, why after “while” statment (and recived signal from second thread) we need to unlock the mutex, when signal makes that mutex is locked? I get it from a lot examples from we…
How to write integers from file to buffer using POSIX pthreads in Linux?
I want to write a program to get integers from a file and put them in a buffer using multiple threads. The buffer size is 20 and there are 100 integers. I can’t use for-loop because one thread should read one integer at a time. As an example, if we are using one thread that thread must run 100 times
pthread mutex does not work correctly on macOS
Currently I am learning POSIX threads on Linux. The following example is counting how many 3(int) there are in an integer array, which returns a correct answer on CentOS 6.5 but a wrong one on macOS 10.12.4. The answer should be 64 * 1024 * 1024 / 2 = 67,108,864 / 2 = 33,554,432. But the answer on macOS is
How costly is a low contention mutex in terms of cpu
I have some C code (on Linux) that needs to run in a thread safe manner. It’s very low contention but I have to guard it to ensure correctness. I have the option of using local variables to guard it (which will work based on how the code is written) but since I have a mutex variable, which is the
Semaphores and Mutex behaviour doubts
Do semaphores and mutexes synchronize both threads and processes, or only threads, or only processes? Edit: my mistake it’s C, not shell. Since I programmed through the shell I mistook it. And one more thing: are communication and synchronization the same? Answer SysV IPC semaphores synchronize between …
Why is interrupt disabled between spin_lock and spin_unlock in Linux?
I was reading the implementation of Linux semaphores. Due to atomicity, signal and wait (up and down in the source code) use spin locks. Then I saw Linux disabled interrupt in spin_lock_irqsave and reenabled interrupt in spin_unlock. This confused me. In my opinion, there is really no point disabling interrup…
print odd and even numbers using 2 threads using mutex in C
Two threads in the program alternately print even and odd numbers till 100. I have tried this and it worked. Is there a way to access the value of the shared data inside main and terminate the 2 threads when the value reaches 100 Answer There were few errors, in the code I posted earlier, I corrected those mi…
Is fopen() a thread safe function in Linux?
If I use fopen() call to open a same file in multi-thread, and write data to the file. Should I use a mutex to ensure the data won’t be disordered? Answer If two threads both open the same file with fopen(), they will each have independent file streams (FILE *) backed by independent file descriptors ref…