mutex

Does making a Reentrant Lock static and make it a mutex?

南笙酒味 提交于 2019-12-03 16:59:12
问题 In Brian Goetz's book, Java Concurrency in Practice, his example of a Reentrant lock is programmed like this: Lock lock = new ReentrantLock(); However, I am curious to know if changing the above code to: private static final Lock lock = new ReentrantLock(); causes the lock to now act as a mutex, or if it is unnecessary and redundant. Thus, does the functionality of this code change if the lock is made private, static, and final? lock.lock(); try { //method stuff } finally { lock.unlock(); }

How to correctly destroy pthread mutex

旧时模样 提交于 2019-12-03 16:16:16
How exactly i can destroy a pthread mutex variable ? Here is what i want to do. I want to have objects (structure variables) cached , which are looked up by key. I want to have minimum granularity of locks here. So i want to have a lock for each object probably embedded in the structure so that i can have object level locking. Now the problem is how to safely destroy these objects ? Looks like first step is to remove the object from the lookup table so that the object is not accessible in future that is fine. I want to free the object from the cache. Now how to destroy/free mutex correctly ?

Fastest Way for Java to write mutexes?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 16:12:01
Mutexes are pretty common in many programming languages, like e.g. C/C++. I miss them in Java. However, there are multiple ways I could write my own class Mutex : Using a simple synchronized keyword on Mutex . Using a binary semaphore . Using atomic variables, like discussed here . ...? What is the fastest (best runtime) way? I think synchronized is most common, but what about performance? Mutexes are pretty common in many programming languages, like e.g. C/C++. I miss them in Java. Not sure I follow you (especially because you give the answer in your question). public class SomeClass {

std::mutex lock hangs when overriding the new operator

若如初见. 提交于 2019-12-03 16:08:27
We have an internal memory manager that we use with one of our products. The memory manager overrides the new and delete operators, and works fine in single-threaded appliations. However, I'm now tasked to make it work with multi-threaded applications too. To my understanding the following pseudo code should work, but it hangs on a spin, even with try_lock() . Any ideas? Update #1 Causes "Access Violation": #include <mutex> std::mutex g_mutex; /*! \brief Overrides the Standard C++ new operator \param size [in] Number of bytes to allocate */ void *operator new(size_t size) { g_mutex.lock(); //

Ruby Semaphores?

烂漫一生 提交于 2019-12-03 15:48:39
问题 I'm working on an implementation of the "Fair Barbershop" problem in Ruby. This is for a class assignment, but I'm not looking for any handouts. I've been searching like crazy, but I cannot seem to find a Ruby implementation of Semaphores that mirror those found in C. I know there is Mutex, and that's great. Single implementation, does exactly what that kind of semaphore should do. Then there's Condition Variables. I thought that this was going to work out great, but looking at these, they

How to use a std::mutex in a class context

自古美人都是妖i 提交于 2019-12-03 13:33:06
i'm having some trouble using a C++11 std::mutex in my class I have a variable called semaphore of type std::mutex. So I positioned my semaphore.lock() and semaphore.unlock() before and after my critical section class Database { public: Database(string, string, string, string); virtual ~Database(); struct sMyHome getMyHome(void); struct sPhysical getPhysical(int); struct sSystemInfo getSystemInfo(void); void commitSystemInfo(struct sSystemInfo); struct sTSensors getTSensors(int); struct sWireless getWireless(int); struct sWirelessConf getWirelessConf(int); struct sWirelessStatus

Does mutex_unlock function as a memory fence?

别说谁变了你拦得住时间么 提交于 2019-12-03 13:19:36
问题 The situation I'll describe is occurring on an iPad 4 (ARMv7s), using posix libs to mutex lock/unlock. I've seen similar things on other ARMv7 devices, though (see below), so I suppose any solution will require a more general look at the behaviour of mutexes and memory fences for ARMv7. Pseudo code for the scenario: Thread 1 – Producing Data: void ProduceFunction() { MutexLock(); int TempProducerIndex = mSharedProducerIndex; // Take a copy of the int member variable for Producers Index

Will killed process/thread release mutex?

时间秒杀一切 提交于 2019-12-03 12:55:09
Several processes access shared memory, locking it with the mutex and pthread_mutex_lock() for synchronization, and each process can be killed at any moment (in fact I described php-fpm with APC extension, but it doesn't matter). Will the mutex be unlocked automatically, if the process locked the mutex and then was killed? Or is there a way to unlock it automatically? Edit: As it turns out, dying processes and threads have similar behavior in this situation, which depends on robust attribute of mutex . Aaron Digulla That depends on the type of mutex. A "robust" mutex will survive the death of

Pthread Mutex: pthread_mutex_unlock() consumes lots of time

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:40:55
问题 I wrote a multi-thread program with pthread, using the producer-consumer model. When I use Intel VTune profiler to profile my program, I found the producer and consumer spend lots of time on pthread_mutex_unlock. I don't understand why this happened. I think threads may wait a long time before they can acquire a mutex, but releasing a mutex should be fast, right? The snapshot below is from Intel VTune. It shows the codes where consumer tries to fetch an item from the buffer, and time consumed

Locking multiple mutexes

岁酱吖の 提交于 2019-12-03 10:30:37
I'm wondering if it's possible to lock multiple mutexes at the same time, like: Mutex1.Lock(); { Mutex2.Lock(); { // Code locked by mutex 1 and 2. } Mutex2.Unlock(); // Code locked by mutex 1. } Mutex1.Unlock(); It would be very useful for some situations. Thanks. It is possible but the order of locking must be consistent throughout the application otherwise deadlock is a likely result (if two threads acquire the locks in opposite order then each thread could be waiting on the other to release one of the locks). Recommend using a scoped lock and unlock facility for exception safety, to ensure