boost-mutex

Boost shared memory and synchronized queue issue/crash in consumer process

喜夏-厌秋 提交于 2020-12-06 12:35:59
问题 I'm trying to consume from a child process a synchronized queue in c++. I'm using this synchronized queue in C++ () (http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-c-i.html) I modified the queue to be serializable in boost and also replaced the used boost::mutex io_mutex_ to use instead an inteprocess mutex (thanks @Sehe) boost::interprocess::interprocess_mutex io_mutex_ And when locking I changed every line that has boost::mutex::scoped_lock lock(io_mutex_); to scoped

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

荒凉一梦 提交于 2020-01-13 08:06:08
问题 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<boost::mutex> and everything worked fine. When I use std::mutex together with std::lock_guard<std::mutex> instead, I get the following error when returning from main() . Unhandled exception at 0x7721E3BE (ntdll.dll) in GrabberTester.exe: 0xC0000005: Access violation reading location 0xA6A6B491.

mutex and threads independence

半世苍凉 提交于 2019-12-24 11:44:39
问题 I run the following program on a 32 cores computer: #include<iostream> #include<algorithm> #include<boost/thread.hpp> using namespace std; boost::thread_group g; boost::mutex _mtx; class A{ public: void foo() { for(int ix = 0; ix < 10000000; ++ix) vec.push_back(ix); sort(vec.rbegin(), vec.rend()); } private: vector<int> vec; }; void thread_fun() { A a; _mtx.lock(); //line 24 a.foo(); _mtx.unlock(); //line 26 } int main() { g.add_thread(new boost::thread(thread_fun)); g.add_thread(new boost:

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

一世执手 提交于 2019-12-17 16:29:27
问题 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 too many threads I have a crash. My best idea for a solution is to make sure that there are never, say, more than three instances running at the same time. (Is this a good idea?) And my current best idea for implementing that is to use a boost

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

Boost scoped_lock failed every time

人盡茶涼 提交于 2019-12-11 14:22:40
问题 In a class, I want to use a mutex over a function like this void Agent::notify(Packet& packet, Peer peer) { boost::mutex::scoped_lock lock(mutex_); ... } No problem at the compilation process. But when I execute the program, boost always fail at this line saying : terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >' what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument Abandon

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

ⅰ亾dé卋堺 提交于 2019-12-10 04:57:57
问题 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 upgradeable/shared lock and then both attempt to upgrade, at which point neither can proceed because the other has a shared lock. What I want is to obtain the exclusive lock first and then downgrade to a shared lock without releasing the lock

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

一曲冷凌霜 提交于 2019-12-05 08:57:18
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 upgradeable/shared lock and then both attempt to upgrade, at which point neither can proceed because the other has a shared lock. What I want is to obtain the exclusive lock first and then downgrade to a shared lock without releasing the lock completely. I cannot find an example of this. Any ideas? Boost offers this functionality through the

boost::named_mutex: Safely cleaning up when last process closes

走远了吗. 提交于 2019-12-02 08:20:34
问题 I have a resource which I need to protect access to within a process, and across multiple processes. I've managed this by creating a named mutex via boost::interprocess:named_recursive_mutex , and it works great. #include <boost/interprocess/sync/named_recursive_mutex.hpp> boost::interprocess::named_recursive_mutex mut( boost::interprocess::open_or_create, "MY_SHARED_MUTEX_123"); However, it's to my understanding that this should be cleaned up eventually via remove() , ie: mut.remove("MY

boost::named_mutex: Safely cleaning up when last process closes

对着背影说爱祢 提交于 2019-12-02 05:07:14
I have a resource which I need to protect access to within a process, and across multiple processes. I've managed this by creating a named mutex via boost::interprocess:named_recursive_mutex , and it works great. #include <boost/interprocess/sync/named_recursive_mutex.hpp> boost::interprocess::named_recursive_mutex mut( boost::interprocess::open_or_create, "MY_SHARED_MUTEX_123"); However, it's to my understanding that this should be cleaned up eventually via remove() , ie: mut.remove("MY_SHARED_MUTEX"); However, this call appears to completely clobber the mutex, rather than check/decrement a