Questions tagged [scoped-lock]

29 questions
23
votes
2 answers

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…
Kai Petzke
  • 2,150
  • 21
  • 29
18
votes
2 answers

Boost Mutex Scoped Lock

I was reading through a Boost Mutex tutorial on drdobbs.com, and found this piece of code: #include #include #include #include boost::mutex io_mutex; void count(int…
Joe Wilcoxson
  • 1,420
  • 3
  • 11
  • 21
11
votes
4 answers

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…
Kokos
  • 442
  • 4
  • 12
7
votes
3 answers

"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…
Atmocreations
  • 9,923
  • 15
  • 67
  • 102
7
votes
1 answer

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…
thenail
  • 438
  • 1
  • 5
  • 10
5
votes
3 answers

std::scoped_lock behaviour with a single mutex

Context: I know that std::lock_guard became kind of deprecated since the arrival of c++17 with std::scoped_lock. I also know that std::scoped_lock is preferred since it can handle several mutexes and uses a deadlock avoidance algorithm the same way…
Fareanor
  • 5,900
  • 2
  • 11
  • 37
4
votes
1 answer

How to understand destructor of scoped_lock?Does cppreference make a mistake?

~scoped_lock() { std::apply([](auto&... __m) { (__m.unlock(), ...); }, _M_devices); } How to understand [](auto&... __m) { (__m.unlock(), ...);? I don't understand the ... in lambda and I don't know how this implement release mutexes in reverse…
3
votes
1 answer

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; …
2
votes
3 answers

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…
Kiril
  • 39,672
  • 31
  • 167
  • 226
2
votes
0 answers

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…
Mercer
  • 148
  • 1
  • 10
2
votes
3 answers

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;…
Patrizio Bertoni
  • 2,582
  • 31
  • 43
2
votes
3 answers

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…
Tom
  • 43,810
  • 29
  • 138
  • 169
1
vote
1 answer

What's the difference between boost::signals2::mutex and boost::thread::mutex?

I'm using scoped_lock and mutex to implement a version of a BlockingQueue posted in a different SO question, but there are multiple different precompiled headers for both of them in boost. scoped_lock is available through…
Kiril
  • 39,672
  • 31
  • 167
  • 226
1
vote
1 answer

Boost::thread, glut and data sharing

I think I have a problem in my program. I must create an object that continuosly communicate with an external tracking system and get coordinates of point from it. I wrapped this class inside a boost::thread and before the first calls to my Glut…
linello
  • 8,451
  • 18
  • 63
  • 109
1
vote
1 answer

C++ std::scoped_lock: What happens if the lock blocks?

I am interested to know more about how the std::scoped_lock operates. I am making some modifications to some thread-unsafe code by adding a mutex around a critical section. I am using a std::scoped_lock to do this. There are two possible things…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
1
2