Questions tagged [mutex]

A mutex ("mutual exclusion") is a mechanism to ensure integrity when the same data or resource is accessed (in particular, changed) concurrently from several threads.

A mutex ("mutual exclusion") is a mechanism to ensure integrity when the same data or resource is accessed (in particular, changed) concurrently from several threads.

Usually when the term mutex is used, it refers to a synchronisation primitive functionally identical to a binary semaphore. As OS-level synchronisation is often rather expensive due to context switching, busy-wait solutions based on atomic operations (e.g. mutex or critical section) exist.

See also:

4640 questions
2
votes
1 answer

Debugging deadlock with pthread mutex(linux)

i am facing a deadlock in one of my c application(its a big code) and I was able to debug down the stage where I printed a mutex. It looks like below - {__data = {__lock = 2, __count = 0, …
user516984
  • 21
  • 1
  • 2
2
votes
1 answer

Pthreads - turning sequential programs into parallel

I'm simulating "Conway's Game of Life" with C++, where a 2d matrix signifies the board and a 0 is an empty cell while a 1 is a living cell. I originally wrote this sequentially, and tried to make it parallel with pthreads. For some reason though,…
2
votes
1 answer

Should the mutex manipulation functions parameter be `volatile`?

I am working with custom embedded real-time OS having some home-brew threading and synchronization facility. The mutual exclusion is implemented in a way similar to the outlined below: typedef int Mutex; #define MUTEX_ACQUIRED 1 #define…
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
2
votes
1 answer

Will a thread waiting on a mutex get the ownership, immediately after mutex_unlock() by other thread?

I have two threads - threadA & threadB. If B is waiting for mutex, which is owned by A, Will it get the ownership immediately after A unlocks it, assuming it has higher priority than A ? This not a question on who gets the lock when multiple threads…
Hari
  • 153
  • 8
2
votes
1 answer

Very slow mutex in LLVM/OpenMP

I wrote code to test the performance of openmp on win (Win7 x64, Corei7 3.4HGz) and on Mac (10.12.3 Core i7 2.7 HGz). In xcode I made a console application setting the compiled default. I use LLVM 3.7 and OpenMP 5 (in opm.h i searched define…
D.Sedov
  • 23
  • 5
2
votes
1 answer

write fails in same file from two processes

My Dll writes data into a file "Sample.txt". If the Dll is loaded by two processes, then the Sample.txt will be written by both the processes. In that case, only the process which writes first into file keeps on writing into it. I couldnt see the…
Sel_va
  • 588
  • 5
  • 25
2
votes
2 answers

Visual Basic - Using Nested Mutex's in a threaded application - Risks and Suggestions

Good evening fellow stackers, I have a mutex and threaded related question about // ¦ = Run one or the other per call to routine |GROUP A| GROUP B | | A & B GROUP MUTEX | |=======|============| |A1 & A2¦ B1 & |B2|B3| |MUTEX| …
Tom 'Blue' Piddock
  • 2,131
  • 1
  • 21
  • 36
2
votes
3 answers

Assure, function runs under lock_guard

I have a function foo() which is called from different places in a heavy multi-threaded environment. It is modifying some sensitive data but due to the complexity I cannot use inside this function std::lock_guard... - this must be assured by the…
Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
2
votes
4 answers

Lock two mutex at same time

I'm trying to implement a multi-in multi-out interthread channel class. I have three mutexes: full locks when buffer is full. empty locks when buffer is empty. th locks when anyone else is modifying buffer. My single IO program looks…
ZisIsNotZis
  • 1,570
  • 1
  • 13
  • 30
2
votes
0 answers

unlocking mutex after returning object vector variable

Some Background: I'm programming a simple taxi center project (c++, on linux), and I was asked to add multithreading in order to support some operations (I'm new to multithreading). One of the operations I need to implement is calculating drivers…
David
  • 39
  • 7
2
votes
0 answers

Golang protecting struct with mutex, race is still detected with locks

I have a struct defined as follows: type transactionList map[string]Transaction type ActiveTransactions struct { mtx sync.Mutex trm transactionList } This struct has several methods defined: func (act *ActiveTransactions) NewTransaction(id…
Vincent Chang
  • 65
  • 1
  • 4
2
votes
1 answer

Can I use no thread synchronization here?

I am researching mutexes. I come up with this example that seems to work without any synchronization. #include #include #include constexpr size_t COUNT = 10000000; int g_x = 0; void p1(){ for(size_t i = 0; i <…
Nick
  • 9,962
  • 4
  • 42
  • 80
2
votes
3 answers

Automatically release mutex on crashes in Unix

I have two programs interacting via a shared memory segment. When using the segment for read or write, they hold a lock. Should either of them crash (get killed, typically - possibly with something untrappable) in a critical section, I would like…
ijw
  • 4,376
  • 3
  • 25
  • 29
2
votes
0 answers

Issue with Mutex getting lock before it was released and how to distribute locks equally?

The method GetItemSearchResponse will be called by multiple console application. But I wanted to call this method one by one. So I applied mutex. So that this method will be locked for other threads. public class AWSItemSearchClient :…
Manjay_TBAG
  • 2,176
  • 3
  • 23
  • 43
2
votes
2 answers

How does pthread mutex unlock work? And do threads come up at the same time?

I wanna ask you some basic thing but it really bothers me a lot. I'm currently studying 'pthread mutex' for system programming and as far as I know, when 'pthread_mutex_lock' is called only current thread is executed not any others. Can I think like…
Hyunil Kang
  • 41
  • 1
  • 6