Questions tagged [boost-mutex]

The mutual exclusion classes of the Boost.Thread library are designed to serialize access to resources shared between C++ threads.

89 questions
15
votes
2 answers

How to use a lock_guard with try_lock_for

I can use boost::lock_guard to acquire a lock on a boost::mutex object and this mechanism will ascertain that once the boost::lock_guard goes out of scope the lock will be released: { boost::lock_guard lock(a_mutex); // Do the…
user8472
  • 3,268
  • 3
  • 35
  • 62
11
votes
4 answers

Unhandled exception when using std::mutex instead of boost::mutex

I try to get rid of some of the boost dependencies in my code and instead use the new C++11 features (Visual Studio 2013). In one of my components I used boost::mutex together with boost::lock_guard and everything worked fine. When I…
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
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
2 answers

How to obtain an exclusive lock *first* and then downgrade to shared without releasing the lock

Stack Overflow has several examples where a function obtains an upgradeable lock first and then obtains exclusive access by upgrading. My understanding is that this can cause deadlocks if not used carefully since two threads may both obtain the…
Elliot Cameron
  • 5,235
  • 2
  • 27
  • 34
6
votes
2 answers

Holding two mutex locks at the same time

I would like to know if there would be any issue if I hold two boost::scoped_locks at the same time. The locks are locking different mutexes. Consider the following example: void foo1() { boost::recursive_mutex::scoped_lock lock(mutex1); …
Petko Six
  • 285
  • 1
  • 3
  • 7
5
votes
1 answer

Why is boost's interprocess mutex not robust on posix?

I'm very new to boost, and just trying to understand a small part of it - the interprocess mutex. AFAIU, there's a robust mutex emulation in the cross platform implementation using file lock:…
Shiva Wu
  • 1,074
  • 1
  • 8
  • 20
5
votes
2 answers

boost::shared_mutex slower than a coarse std::mutex on Linux

I have a std::unordered_map which is subjected to a very read-heavy workload from multiple threads. I could use a std::mutex for synchronization, but since concurrent reads should be fine, I wanted to use a boost::shared_mutex instead. To test the…
Max
  • 21,123
  • 5
  • 49
  • 71
5
votes
2 answers

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

I would like to lock the keys/index in another map like this: std::map pointCloudsMutexes_; pointCloudsMutexes_[index].lock(); However, I am getting the following error: /usr/include/c++/4.8/bits/stl_pair.h:113: error: no…
Raaj
  • 363
  • 1
  • 3
  • 12
5
votes
2 answers

How to limit the number of running instances in C++

I have a c++ class that allocates a lot of memory. It does this by calling a third-party library that is designed to crash if it cannot allocate the memory, and sometimes my application creates several instances of my class in parallel threads. With…
Emil Fredrik
  • 117
  • 1
  • 8
4
votes
5 answers

Manually releasing boost locks?

For the sake of learning combinatorics of boost::thread I'm implementing a simple barrier (BR) for threads which lock a common mutex (M). However, as far as I get it when going to BR.wait() the locks on the mutex are not released, so in order for…
P Marecki
  • 1,108
  • 15
  • 19
3
votes
2 answers

(boost) overhead of a unique_lock when used with a conditional_variable

why does the wait() method of a boost::conditiona_variable require a boost::unique_lock object as parameter and not a simple boost::mutex? Actually, it is not completely clear the purpose of a unique_lock at all. Why should I create another wrapper…
jrbn
  • 303
  • 2
  • 6
3
votes
1 answer

Is there anything Faster than a boost mutex for this code?

Currently in my code I have sections such as this boost::mutex Mymutex void methodA() { boost::mutex::scoped_lock lock(Mymutex); ...... ...... ...... } I read that critical sections are faster than a mutex ? So I am doing something…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
3
votes
1 answer

Boost::thread mutex issue: Try to lock, access violation

I am currently learning how to multithread with c++, and for that im using boost::thread. I'm using it for a simple gameengine, running three threads. Two of the threads are reading and writing to the same variables, which are stored inside…
user1419305
  • 468
  • 8
  • 16
3
votes
1 answer

Boost::mutex performance vs pthread_mutex_t

I was using pthread_mutex_ts beforehand. The code sometimes got stuck. I had a couple of lines of code scattered across functions that I wrapped... pthread_mutex_lock(&map_mutex);// Line 1 //critical code involving reading/writing wrapped around a…
Joshua
  • 1,516
  • 2
  • 22
  • 31
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; …
1
2 3 4 5 6