scoped-lock

Check optional mutex before scoped locking

老子叫甜甜 提交于 2019-12-24 10:56:42
问题 I have a constructor that optionally allows the user to pass a ponter to a Boost mutex. If no mutex is supplied, the member pointer pMyMutex is set to NULL . This gives the user the option of applying some thread safety if they wish. However, I cannot use a scoped_lock with this kind of check for obvious reasons :) if (pMyMutex != NULL) const boost::mutex::scoped_lock l(*pMyMutex); //The lock is already out of scope processStuff(x, y, z); Can anyone suggest a neat and simple solution to such

Looking for a function (or a macro) to return a boost::scoped_lock

好久不见. 提交于 2019-12-24 01:07:10
问题 I'm looking for code shortening idea. I'm using boost::scoped_lock to lock a boost::mutex but I want to shorten the amount of code I'm writing. Currently I have a mutex defined in my class and the member field called _sync . When I want to lock, I have to write: scoped_lock<mutex> lock(_sync); The tricky part is that this is a scoped lock, so I assume that if I write a static function to return the scoped_lock, then it will unlock as soon as it gets out of the function scope of the static

Can scoped_lock lock a shared_mutex in read mode?

十年热恋 提交于 2019-12-21 07:27:07
问题 C++17 introduced both std::shared_mutex and std::scoped_lock . My problem is now, that it seems, that scoped_lock will lock a shared mutex always in exclusive (writer) mode, when it is passed as an argument, and not in shared (reader) mode. In my app, I need to update an object dst with data from an object src . I want to lock src shared and dst exclusive. Unfortunately, this has the potential for deadlock, if a call to another update method with src and dst switched occurs at the same time.

Boost Mutex Scoped Lock

白昼怎懂夜的黑 提交于 2019-12-02 20:40:42
I was reading through a Boost Mutex tutorial on drdobbs.com, and found this piece of code: #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <iostream> boost::mutex io_mutex; void count(int id) { for (int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(io_mutex); std::cout << id << ": " << i << std::endl; } } int main(int argc, char* argv[]) { boost::thread thrd1( boost::bind(&count, 1)); boost::thread thrd2( boost::bind(&count, 2)); thrd1.join(); thrd2.join(); return 0; } Now I understand the point of a Mutex is to prevent two threads