Questions tagged [scoped-lock]

0

votes
0

answer
18

Views

Can scoped_lock lock a shared_mutex in read mode?

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...
Kai Petzke
2

votes
3

answer
496

Views

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

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 lock(_sync); The tricky pa...
Kiril
6

votes
3

answer
1.7k

Views

“Nested” scoped_lock

My shortened, simplified class looks as follows: class A { public: // ... methodA(); methodB(); protected: mutable boost::mutex m_mutex; sometype* m_myVar; } A::methodA( int someParam ) { boost::mutex::scoped_lock myLock(m_mutex); m_myVar->doSomethingElse(); } A::methodB( int someParam ) { boost::m...
Atmocreations
4

votes
1

answer
1.9k

Views

boost::interprocess::scoped_lock application crash inside lock

I'm using boost::interprocess::scoped_lock, if the application crash for some reason inside the scope the mutex isn't released. Next time the application is executed (without restarting the computer), the mutex is locked. How's this intended to work? I give a simple example of the code below. { boos...
thenail
2

votes
3

answer
2.5k

Views

boost scoped_lock. Will this lock?

solved I changed the bfs::directory_iterator Queue to a std::string queue, and surprisingly solved the problem. Hi, I have a gut feeling that i'm doing things wrong. I've implemented (or attempted to) the thread pool pattern. N threads read from a Queue, but i'm having some trouble. Here's what i go...
Tom
1

votes
1

answer
538

Views

Check optional mutex before scoped locking

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 obv...
learnvst
3

votes
1

answer
530

Views

boost-threads: How can I pass a scoped_lock to a callee?

I'm new to the boost threads library. I have a situation where I acquire a scoped_lock in one function and need to wait on it in a callee. The code is on the lines of: class HavingMutex { public: ... private: static boost::mutex m; static boost::condition_variable *c; static void a(); static void b(...
5

votes
4

answer
961

Views

Is there idiomatic scoped semantics in golang?

I wonder if there is any idiomatic way to represent scoped semantics. By scoped I mean things like: scoped mutex (oneliner instead of explicit Lock + deffered Unlock), logging function (or any code block) entrance and exit, measuring execution time. Example code for first two bullets: package main i...
Kokos
2

votes
0

answer
112

Views

Using the same mutex for unique_lock and scoped_lock

Is it appropriate to use both a unique_lock and a scoped_lock with the same mutex? To allow for use of cv.wait and optional unlocking while also providing scope-bound safety. For example; std::mutex mut; //thread: std::condition_variable cv; std::unique_lock lock(mut); cv.wait(lock); std::scoped_loc...
Mercer
2

votes
3

answer
93

Views

boost::scoped_lock RAII behaviour

From a Container class, I'd like to lock a vector of boost::mutex, each one owned by a Controlled instance (weird code design, but for MWE purpose only). // std::vector _vData; void Container::main_method() { for (int i=0; i_mutex); this->processing(i); } // precondition for post_processing(): all p...
Patrizio Bertoni