mutex

Concurrent File write between processes

限于喜欢 提交于 2019-12-02 20:04:43
问题 I need to write log data into a single file from different processes. I am using Windows Mutex which needs Common Language Runtime support for it. Mutex^ m = gcnew Mutex( false,"MyMutex" ); m->WaitOne(); //... File Open and Write .. m->ReleaseMutex() Do I really need to change from C++ to C++/CLI for synchronization? It is ok if the atomic is not used. But I need to know whether using this Mutex will slow down the performance compared to local mutex. 回答1: Adding CLR support to your C++

What is Test-and-Set used for?

好久不见. 提交于 2019-12-02 19:36:11
After reading the Test-and-Set Wikipedia entry , I am still left with the question "What would a Test-and-Set be used for?" I realize that you can use it to implement Mutex (as described in wikipedia), but what other uses does it have? moonshadow You use it any time you want to write data to memory after doing some work and make sure another thread hasn't overwritten the destination since you started. A lot of lock/mutex-free algorithms take this form. A good example is "increment." Say two threads execute a = a + 1 . Say a starts with the value 100 . If both threads are running at the same

How are mutex and lock structures implemented?

谁说胖子不能爱 提交于 2019-12-02 19:12:31
I understand the concept of locks, mutex and other synchronization structures, but how are they implemented? Are they provided by the OS, or are these structures dependent on special CPU instructions for the CPUs MMU? James Black You may want to look at these links, but the main one is the Test-and-set on Wikipedia: http://en.wikipedia.org/wiki/Test-and-set How are mutexes implemented? You can also look at this patent: http://www.faqs.org/patents/app/20080222331 Most mutual exclusion and synchronization mechanisms use hardware atomic operations, as others have pointed out. However, it is

CLOCK_MONOTONIC and pthread_mutex_timedlock / pthread_cond_timedwait

£可爱£侵袭症+ 提交于 2019-12-02 19:10:23
The pthread_mutex_timedlock documentation says that abs_timeout takes a CLOCK_REALTIME . However, we all know that it is inappropriate for timing a specific duration (due to system time adjustments). Is there a way to make pthread lock timeout on CLOCK_MONOTONIC that is portable? The same goes with pthread_cond_timedwait. Having looked at the documentation and pthread.h , I can't find a way to make pthread_mutex_timedlock use CLOCK_MONOTONIC so I assume that's not (currently) possible. For pthread_cond_timedwait , however, you can use code like this: pthread_condattr_t attr; pthread_cond_t

pthreads : pthread_cond_signal() from within critical section

强颜欢笑 提交于 2019-12-02 18:47:33
I have the following piece of code in thread A, which blocks using pthread_cond_wait() pthread_mutex_lock(&my_lock); if ( false == testCondition ) pthread_cond_wait(&my_wait,&my_lock); pthread_mutex_unlock(&my_lock); I have the following piece of code in thread B, which signals thread A pthread_mutex_lock(&my_lock); testCondition = true; pthread_cond_signal(&my_wait); pthread_mutex_unlock(&my_lock); Provided there are no other threads, would it make any difference if pthread_cond_signal(&my_wait) is moved out of the critical section block as shown below ? pthread_mutex_lock(&my_lock);

What's the difference between “mutex” and “lock”?

牧云@^-^@ 提交于 2019-12-02 18:09:24
I am very confused about the difference between a lock and mutex. In Boost docs, it says, Lock Types Class template lock_guard Class template unique_lock Class template shared_lock Class template upgrade_lock Class template upgrade_to_unique_lock Mutex-specific class scoped_try_lock Mutex Types Class mutex Typedef try_mutex Class timed_mutex Class recursive_mutex Typedef recursive_try_mutex Class recursive_timed_mutex Class shared_mutex In another article, I see functions like this, boost::shared_mutex _access; void reader() { boost::shared_lock< boost::shared_mutex > lock(_access); // do work

Monitor vs Mutex

会有一股神秘感。 提交于 2019-12-02 17:32:15
I read that mutex is a semaphore with value 1 (binary semaphore) which is used for enforcing mutual exclusion. I read this link Semaphore vs. Monitors - what's the difference? which says that monitor helps in achieving mutual exclusion. Can anyone tell me the difference between mutex and monitor as both are actually doing the same thing Since you haven't specified which OS or language/library you are talking about, let me answer in a generic way. Conceptually they are the same. But usually they are implemented slightly differently Monitor Usually, the implementation of monitors is faster/light

How to lock (Mutex) in NodeJS?

扶醉桌前 提交于 2019-12-02 16:42:01
问题 There are external resources (accessing available inventories through an API) that can only be accessed one thread at a time. My problems are: NodeJS server handles requests concurrently, we might have multiple requests at the same time trying to reserve inventories. If I hit the inventory API concurrently, then it will return duplicate available inventories Therefore, I need to make sure that I am hitting the inventory API one thread at a time There is no way for me to change the inventory

Mutex not correctly used? Continuation of past questions

江枫思渺然 提交于 2019-12-02 16:33:18
问题 I have a second question that is a continuation of this thread : How Does Windows Pick memory for threads? void ThreadTest(LPTSTR* Pointer, mutex* MutexPtr) { MutexPtr->lock(); wcout << *Pointer << endl; MutexPtr->unlock(); return; } void FakeMain() { mutex Mut; mutex* MutPtr = &Mut; LPTSTR Image = L"First String"; LPTSTR* StrPointer = &Image; thread Test(ThreadTest, StrPointer, MutPtr); MutPtr->lock(); if (true) { NewStringFunction(Image); MutPtr-unlock() // Added with the comment of Mike

When are lock free data structures less performant than mutual exclusion (mutexes)?

孤者浪人 提交于 2019-12-02 16:23:52
I read somewhere (can't find the page anymore) that lock free data structures are more efficient "for certain workloads" which seems to imply that sometimes they're actually slower or the gain from them can be zero in some situations. Taking the ~100 cycle hit of a lock instruction to do an atomic op sounds plenty faster to me than going to sleep and waiting for the scheduler to wake the process back up, so it's not obvious to me under what circumstances a lock free data structure would be less preferable than old fashioned mutexes. If the lock is available 99% of the time and the process