Questions tagged [mutex]

19 questions
27
votes
5 answers

Is "releases mutexes in reverse order" required to make this deadlock-prevention method work?

Operating System Concepts says 7.4.4 Circular Wait The fourth and final condition for deadlocks is the circular-wait condition. One way to ensure that this condition never holds is to impose a total ordering of all resource types and to require that…
Tim
  • 5,405
  • 7
  • 48
  • 84
13
votes
2 answers

How to test and benchmark mutex implementations

As the title says: How do you properly test and benchmark different implementations of mutexes in c++? Essentially I wrote my own std::mutex like class for a project running on a 2 core, armv7 with the aim to minimize the overhead in the uncontested…
MikeMB
  • 244
  • 1
  • 9
8
votes
5 answers

Implementing a hash table with true concurrency

This was recently a question I was asked in a screening and it got me thinking. I never totally settled on how the conversation ended, and I did some digging. None of the answers I saw seemed satisfactory to me. The question is essentially "how…
8
votes
2 answers

Mutex vs Semaphore: How to implement them _not_ in terms of the other?

Recently I had to implement a Semaphore using a Mutex and a Conditional Variable (this combination is also known as a Monitor) for an exercise at the university: the Semaphore's decrement operation blocks until its counter is more than zero before…
Qqwy
  • 4,709
  • 4
  • 31
  • 45
6
votes
1 answer

Specify that a method needs a mutex held when calling

Current situation Right now I have a method like Data lookupData(Key id) { std::lock_guard lock(m_mutex); auto it = m_dict.find(id); if(it == m_dict.end()) { return Data::Empty; } else { return…
Daniel McLaury
  • 352
  • 1
  • 7
4
votes
1 answer

Simple solution for calling a function only on one thread, queuing waiting calls?

Let’s assume there is a function EventHandler that is called on multiple threads at different points in time. EventHandler has to call SomeOtherFunction, but these calls shall only happen on one thread at a time. That means: If a call to…
Martin
  • 466
  • 5
  • 12
3
votes
2 answers

When should one utilize Atomic Operations?

I have never used atomic operations, and remain largely ignorant of how to utilize them. And yet, I often come across objects like this when peering into Qt's backend: https://doc.qt.io/qt-6/qatomicinteger.html This leads me to question if there is…
Anon
  • 3,565
  • 3
  • 27
  • 45
3
votes
0 answers

Release Phase in Lamport's Mutual Exclusion Algorithm

I was studying Lamport's mutual exclusion algorithm from the original paper, and noticed a difference in the release phase to the one given in Wikipedia. In the original paper, author states that : To release the resource, process Pi removes any…
Varun Hegde
  • 181
  • 3
2
votes
1 answer

Make multiple data structures thread safe without multiple mutexes

In my application, I have a class which I call a "Signal" (Think Qt signals if you are familiar with that) that represents a one-to-many connection. Objects can register handlers to the signal which, when "emitted" calls each of the handlers. This…
Patrick Wright
  • 551
  • 3
  • 11
2
votes
0 answers

Testing an algorithm for safety in mutual exclusion

I am attempting to write a code to test Lamport's Mutual Exclusion algorithm for safety as a correctness measure. I am running the alogrithm on a single core cpu machine with multiple processes sharing the same processor clock. One of ways I was…
1
vote
2 answers

design pattern to avoid deadlock with mutex in golang

What would the appropriate design pattern to avoid deadlock when several functions use the same mutex ? It is quite easy to forget what method uses the lock and so it happens that you call a function that do a mutex.Lock() inside a function that…
cylon86
  • 111
  • 3
1
vote
2 answers

Why is it that "the process that locks the mutex must be the one to unlock it "?

Here are some discussions about mutex (lock) and binary semaphore from two OS books. Stalling's Operating Systems book says A concept related to the binary semaphore is the mutex . A key difference between the two is that the process that locks…
Tim
  • 5,405
  • 7
  • 48
  • 84
1
vote
1 answer

Small Mutex Implementation?

The relatively large size of std::mutex on different platforms (e.g. 40 or even 80 bytes) is often explained as ensuring the mutexes don't occupy the same cache-lines and so don't incur logically unnecessary fetches and synchronization…
Persixty
  • 345
  • 2
  • 10
0
votes
1 answer

Difference between outcome of using mutex/spinlock/semaphore in this deadlock situation?

I'm not sure if my answers to the following exercise is correct: Suppose the program is run with two threads. Further suppose that the following sequence of events occurs: Time Thread 0 Thread 1 0 …
0
votes
1 answer

Is a mutex lock always implemented as spin waiting?

Is a mutex lock always implemented as spin waiting? Can a mutex lock be implemented as block waiting? (Operating System Concepts section 5.4 only mentions the implementation by spin waiting. See below.) (For comparison, a semaphore's waiting can be…
Tim
  • 5,405
  • 7
  • 48
  • 84
1
2