Skip to main content

Questions tagged [mutex]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
2 answers
883 views

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 ...
cylon86's user avatar
  • 111
0 votes
1 answer
125 views

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 ...
Lilian Shi's user avatar
3 votes
2 answers
3k views

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/...
Anon's user avatar
  • 3,613
6 votes
1 answer
1k views

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<std::mutex> lock(m_mutex); auto it = m_dict.find(id); if(it == m_dict.end()) { ...
Daniel McLaury's user avatar
2 votes
1 answer
422 views

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 ...
Patrick Wright's user avatar
0 votes
1 answer
1k views

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....
Tim's user avatar
  • 5,495
1 vote
2 answers
2k views

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 ...
Tim's user avatar
  • 5,495
-1 votes
1 answer
165 views

is there difference between releasing two semaphores in the same or reverse order as they are acquired?

Generally, if two semaphores are acquired one after the other, is there difference between releasing them in the same order or the reverse order as they are acquired? In the following solution to the ...
Tim's user avatar
  • 5,495
27 votes
5 answers
7k views

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 ...
Tim's user avatar
  • 5,495
-2 votes
2 answers
1k views

what would happen if you signal() before wait()?

A bakery shop has to provide a stream of muffins for customers. The muffins are made by a baker in the kitchen and placed on a conveyor belt. The conveyor belt carries the muffins to where the ...
Callum Bell's user avatar
8 votes
5 answers
8k views

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 ...
kiss-o-matic's user avatar
0 votes
0 answers
53 views

Pausing threads calling the same method by making them try to lock an already locked mutex

I am confronted to a question regarding synchronisation between threads having shared data. This is a stripped down versino of my class : class Foo { private: boost::mutex m_mutex; std:vector&...
drbradock's user avatar
0 votes
1 answer
106 views

Multithreading - 2 active running threads calling each other without blocking itself

I have an implemented UDP receiver which is continuously receiving DTN packets (UDP encapsulated) and performing some operations on them. This whole process is working on a single main thread. However ...
Caspian's user avatar
  • 13
2 votes
0 answers
188 views

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 ...
Varun Hegde's user avatar
3 votes
0 answers
55 views

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 : ...
Varun Hegde's user avatar
4 votes
1 answer
684 views

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 ...
Martin's user avatar
  • 476
15 votes
2 answers
2k views

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 ...
MikeMB's user avatar
  • 264
1 vote
1 answer
1k views

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 ...
Persixty's user avatar
  • 355
8 votes
2 answers
5k views

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 ...
Qqwy's user avatar
  • 4,857