mutex

Implement a high performance mutex similar to Qt's one

橙三吉。 提交于 2019-12-03 09:13:12
问题 I have a multi-thread scientific application where several computing threads (one per core) have to store their results in a common buffer. This requires a mutex mechanism. Working threads spend only a small fraction of their time writing to the buffer, so the mutex is unlocked most of the time, and locks have a high probability to succeed immediately without waiting for another thread to unlock. Currently, I have used Qt's QMutex for the task, and it works well : the mutex has a negligible

custom RAII C++ implementation for scoped mutex locks

你离开我真会死。 提交于 2019-12-03 08:21:56
问题 I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex. In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked. Any implementation available? I don't want to re-invent the wheel. I need to use pthreads. resource acquisition is initialization == “RAII” 回答1: Note This is an old answer. C++11 contains better helpers that are more platform independent: std::lock_guard std::mutex

rails - implementing a simple lock to prevent user's from editing the same data concurrently

混江龙づ霸主 提交于 2019-12-03 07:56:44
问题 I have an app where I need to prevent users from editing data while it is being edited by a different user. I'm trying to think of the best way to do it and wanted to ask for ideas. So far, I've created a settings model that stores application wide configuration on the db in key/value pairs. So, for the lock, I have a settings instance that's called LOCKED_TABLE_UID, and it stored the user_id of the user editing the table or null (nil) if the table is free. >> lock = Setting.find_by_key(

Ruby Semaphores?

≯℡__Kan透↙ 提交于 2019-12-03 06:10:08
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 require a Mutex for every wait call, which looks to me like I can't put numerical values to the semaphore

对BOOST 中同步互斥的一些理解

只谈情不闲聊 提交于 2019-12-03 05:56:02
对BOOST 中同步互斥的一些理解 首先,BOOST中有4种有关互斥量得概念。 1.LOCKABLE :仅支持排它型所有权 2.TIMEDLOCKABLE:支持带超时的排它型所有权 3.SHAREDLOCKABLE: 支持带超时的排他型所有权和共享型所有权(读写锁) 4.UPGRADELOCKABLE: 支持带超时的排他型所有权和共享型所有权,以及共享型所有权升级为排他型所有权(升级过程阻塞)(也支持降级) 可以看到2强化自1,3强化自2.4强化自3,支持某一概念则一定支持其强化自的概念。 boost::mutex 实现了LOCKABLE概念 (boost::recursive_mutex 是其递归锁的版本) boost::timed_mutex 实现了TIMEDLOCKABLE概念 (boost::recursive_timed_mutex 是其递归锁的版本) boost::shared_mutex实现了SHAREDLOCKABLE概念 boost::shared_mutex同样实现了UPGRADELOCKABLE概念 出于提供RAII操作风格和安全等其他一些原因BOOST不希望用户直接调用各种MUTEX类型中的相关接口,而是通过它提供的一些LOCK_TYPE来帮助我们调用。 主要的LOCK_TYPE包括: boost::unique_lock<LOCKABLE>

How are mutex and lock structures implemented?

倖福魔咒の 提交于 2019-12-03 05:51:21
问题 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? 回答1: 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 回答2: Most mutual exclusion and

CLOCK_MONOTONIC and pthread_mutex_timedlock / pthread_cond_timedwait

十年热恋 提交于 2019-12-03 05:49:16
问题 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. 回答1: 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.

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

Deadly 提交于 2019-12-03 05:44:00
问题 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:

Thread Synchronisation 101

做~自己de王妃 提交于 2019-12-03 05:02:57
问题 Previously I've written some very simple multithreaded code, and I've always been aware that at any time there could be a context switch right in the middle of what I'm doing, so I've always guarded access the shared variables through a CCriticalSection class that enters the critical section on construction and leaves it on destruction. I know this is fairly aggressive and I enter and leave critical sections quite frequently and sometimes egregiously (e.g. at the start of a function when I

pthreads : pthread_cond_signal() from within critical section

会有一股神秘感。 提交于 2019-12-03 04:27:10
问题 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