mutex

C++ Error linking in consumer file caused by static data field

房东的猫 提交于 2019-12-13 02:06:28
问题 I want to use a static global variable as a mutex. When I try to compile the following code: //header file class __declspec(dllexport) StateConservator { private: StateConservator(); StateConservator(const StateConservator&); protected: const CString m_oldConf; CContainer& m_container; static bool x_mutex; public: StateConservator(CContainer& container, const CString& conf) : m_container(container) , m_oldConf(!x_mutex? container.GetConf():_T("")) { if(!x_mutex) { x_mutex= true; m_container

Boost: mutex unlocking from any thread possible?

☆樱花仙子☆ 提交于 2019-12-12 23:15:12
问题 I started using boost::thread recently (WinXP, VS10, BoostPro) and found that mutex can be unlocked by any thread, not by the thread that owns it only. Additionally it seams that the basic lock_guard + mutex combo is doing some internal counting of multiple lock() and unlock() but it is not a big issue I guess. Does somebody know why it was designed in such a way? Is it on purpose? (or maybe there is something wrong with my build environment / libs?) Example app: #include <iostream> #include

Where is a MutexGuard if I never assign it to a variable?

拈花ヽ惹草 提交于 2019-12-12 19:59:40
问题 I don't understand "where" the MutexGuard in the inner block of code is. The mutex is locked and unwrapped, yielding a MutexGuard . Somehow this code manages to dereference that MutexGuard and then mutably borrow that object. Where did the MutexGuard go? Also, confusingly, this dereference cannot be replaced with deref_mut . Why? use std::sync::Mutex; fn main() { let x = Mutex::new(Vec::new()); { let y: &mut Vec<_> = &mut *x.lock().unwrap(); y.push(3); println!("{:?}, {:?}", x, y); } let z =

Using AppMutex with silent Inno Setup

瘦欲@ 提交于 2019-12-12 18:10:00
问题 I'm using "AppMutex" in an Inno Setup script to make sure the product is not running anymore while installing an update. This basically worke fine. But when running this setup with the "/verysilent" command parameter a dialog box is shown (although being very silent) that the program is still running and should be closed. As the program itself is closed automatically and this may take only up to few seconds to be closed I just want Inno Setup to wait till the mutex was disposed and then start

Ruby - Redis based mutex with expiration implementation

情到浓时终转凉″ 提交于 2019-12-12 17:11:25
问题 I'm trying to implement a memory based, multi process shared mutex, which supports timeout, using Redis. I need the mutex to be non-blocking, meaning that I just need to be able to know if I was able to fetch the mutex or not, and if not - simply continue with execution of fallback code. something along these lines: if lock('my_lock_key', timeout: 1.minute) # Do some job else # exit end An un-expiring mutex could be implemented using redis's setnx mutex 1 : if redis.setnx('#{mutex}', '1') #

Should I protect operations on primitive types with mutexes for being thread-safe in C++?

风流意气都作罢 提交于 2019-12-12 14:23:23
问题 What is the best approach to achieve thread-safety for rather simple operations? Consider a pair of functions: void setVal(int val) { this->_val = val; } int getVal() { return this->_val; } Since even assignments of primitive types aren't guaranteed to be atomic, should I modify every getter and setter in the program in the following way to be thread-safe? void setVal(int val) { this->_mutex.lock(); this->_val = val; this->_mutex.unlock(); } int getVal() { this->_mutex.lock(); int result =

Semaphores makeWater() synchronization

為{幸葍}努か 提交于 2019-12-12 14:07:53
问题 This program claims to solve makeWater() synchronization problem. However, I could not understand how. I am new in semaphores. I would appriciate if you can help me to understand this code. 回答1: So you need to make H2O (2Hs and one O) combinations out of number of simultaneously running H-threads and O-threads. The thing is one 'O' needs two 'H' s. And no sharings between two different water molecules. So assume number of O and H threads start their processes. No O thread can go beyond P(o

Mutex release issues in ASP.NET C# code

邮差的信 提交于 2019-12-12 13:44:24
问题 I'm not exactly sure how to address this issue. I have a mutex that is declared as such: public class MyNamedLock { private Mutex mtx; private string _strLkName; public MyNamedLock(string strLockName) { _strLkName = strLockName; //... mtx = new Mutex(false, _strLkName, out bCreatedNew, mSec); } public bool enterLockWithTimeout(int nmsWait = 30 * 1000) { _nmsWaitLock = nmsWait; //Wait return mtx.WaitOne(nmsWait); } public void leaveLock() { _nmsWaitLock = 0; //Release it mtx.ReleaseMutex(); }

pthread mutex (un)locking over different threads

冷暖自知 提交于 2019-12-12 13:16:07
问题 I have a process where main initializes a mutex calling: MutexInit( pthread_mutex_t *Mutex ) { pthread_mutexattr_t mattr; pthread_mutexattr_init(&mattr); pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK_NP); #ifndef _POSIX_THREAD_PROCESS_SHARED #error "This platform does not support process shared mutex!" #else pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); #endif pthread_mutex_init( Mutex, &mattr ); } main is initially locking mutex M1 and creates then threads T1 and

How to use a boost::mutex as the mapped type in std::map?

会有一股神秘感。 提交于 2019-12-12 11:13:12
问题 I would like to lock the keys/index in another map like this: std::map<int, boost::mutex> pointCloudsMutexes_; pointCloudsMutexes_[index].lock(); However, I am getting the following error: /usr/include/c++/4.8/bits/stl_pair.h:113: error: no matching function for call to 'boost::mutex::mutex(const boost::mutex&)' : first(__a), second(__b) { } ^ It seems to work with std::vector , but not with std::map . What am I doing wrong? 回答1: In C++ before C++11, the mapped type of a std::map must be both