mutex

Mutex names - best practice?

淺唱寂寞╮ 提交于 2019-12-22 04:38:21
问题 Related to this question, what is the best practice for naming a mutex? I realize this may vary with OS and even with version (esp for Windows), so please specify platform in answering. My interest is in Win XP and Vista. 回答1: A really safe name for a global mutex is <a description> + <a GUID> : MyApp Single Instance Mutex : {c96f7db4-d743-4718-bef0-8533a198bcca} By using a name like this there is absolutely no chance someone else will use the same mutex name as your mutex. Sniffing around

Ruby synchronisation: How to make threads work one after another in proper order?

本秂侑毒 提交于 2019-12-21 20:16:09
问题 My problem is that I don't know how synchronise multiple threads using Ruby. The task is to create six threads and start them immediately. All of them should do some work (for example puts "Thread 1" Hi" ) one after another in the order I need it to work. I've tried to work with Mutex, Monitor and Condition Variable, but all of them worked in random order. Could anybody explain how to achieve my goal? After some time of struggling with Mutex and Condition Variable I've achieved my goal. This

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

人盡茶涼 提交于 2019-12-21 15:02:08
问题 Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time /

How to use pthread_mutex_trylock?

我与影子孤独终老i 提交于 2019-12-21 09:07:11
问题 Using trylock: FILE *fp; pthread_mutex_t demoMutex; void * printHello (void* threadId) { pthread_mutex_trylock (&demoMutex); pthread_t writeToFile = pthread_self (); unsigned short iterate; for (iterate = 0; iterate < 10000; iterate++) { fprintf (fp, " %d ", iterate, 4); fprintf (fp, " %lu ", writeToFile, sizeof (pthread_t)); fprintf (fp, "\n", writeToFile, 1); } pthread_mutex_unlock (&demoMutex); pthread_exit (NULL); } and then main (): int main () { pthread_t arrayOfThreadId [5]; int

Django: Simple rate limiting

﹥>﹥吖頭↗ 提交于 2019-12-21 05:24:12
问题 Many of my views fetch external resources. I want to make sure that under heavy load I don't blow up the remote sites (and/or get banned). I only have 1 crawler so having a central lock will work fine. So the details: I want to allow at most 3 queries to a host per second, and have the rest block for a maximum of 15 seconds. How could I do this (easily)? Some thoughts : Use django cache Seems to only have 1 second resolution Use a file based semaphore Easy to do locks for concurrency. Not

pthread_mutex_t VS @synchronized block?

眉间皱痕 提交于 2019-12-21 04:55:28
问题 static pthread_mutex_t gLock; //global pthread_mutex_init(&gLock,NULL); //in init pthread_mutex_lock(&gLock); for(int i=0;i<[message count];i++) CFSetAddValue(mSet, [message objectAtIndex:i]); pthread_mutex_unlock(&gLock); My cocoa application is going in not responding mode with pthread_mutex_t. @synchronized(mSet) { for(int i=0;i<[message count];i++) CFSetAddValue(mSet, [message objectAtIndex:i]); } My application is working fine with synchronized block. Why? 回答1: You're comparing a global

How do you protect a common resource using mutexes?

喜欢而已 提交于 2019-12-21 04:42:05
问题 I have a common resource, which I want 1 and only 1 instance of my application (or it's COM API) to have access to at any time. I have tried to protect this resource using mutexes, but when multiple threads of a host dotnet application try to access the COM object, the mutex doesn't seem to be released. This is the code I have used to protect my resource. repeat Mutex := CreateMutex(nil, True, PChar('Connections')); until (Mutex <> 0) and (GetLastError <> ERROR_ALREADY_EXISTS); try //use

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

烂漫一生 提交于 2019-12-21 04:34:12
问题 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

Will killed process/thread release mutex?

坚强是说给别人听的谎言 提交于 2019-12-21 03:55:15
问题 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

Locking multiple mutexes

ぃ、小莉子 提交于 2019-12-21 03:32:06
问题 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. 回答1: 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