如何在C++中定义向量<boost::mutex>?
how to define a vector<boost::mutex> in C++ ?
我想定义一个boost::互斥的向量:
boost::mutex myMutex ;
std::vector< boost::mutex > mutexVec;
mutexVec.push_back(myMutex);
但是,我在Linux上得到错误:
/boost_1_45_0v/include/boost/thread/pthread/mutex.hpp:33: error:
我在网上找不到解决办法。
谢谢
可以使用boost指针容器:
#include <boost/thread.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
boost::ptr_vector<boost::mutex> pv;
pv.push_back(new boost::mutex);
ptr_vector获得其指针的所有权,以便在适当的时候删除它们,而不会产生智能指针可能带来的任何开销。
复制构造函数是私有的。你不应该复制互斥锁
而不是使用:
boost::mutex *myMutex = new boost::mutex();
std::vector< boost::mutex *> mutexVec;
mutexVec.push_back(myMutex);
,如果您不想自己管理内存,请使用boost::shared_ptr<boost::mutex>
而不是boost::mutex*
boost::mutex
不能存储在vector
中,因为它不是可复制的。正如peter的回答中提到的,可以将指向互斥锁的指针存储在vector
中,但是您真的应该重新考虑依赖于这些东西的设计。请记住,vector
本身没有任何线程要求,并且尝试做任何修改vector的操作都不是线程安全的操作。
相关文章:
- BOOST :: MUTEX版本与调试构建
- 使用Boost ::与包含Boost :: Mutex的类绑定
- boost::interprocess_mutex与进程本地boost::mutex
- Boost Mutex implementation for Windows
- 为什么我必须有一个 boost::mutex for boost::condition_variable
- boost::mutex和boost::timed_mutex的性能差异
- 如何使用boost::mutex作为std::map中的映射类型
- boost::mutex::timed_lock not on Linux
- 与 boost::interprocess_mutex 相比,为什么不在共享内存中使用 boost::mutex
- boost::mutex 无法帮助避免C++程序中的竞争条件
- 为什么 boost::mutex 比 std::mutex 快 vs2013.
- boost::mutex 在 Visual Studio 中支持 try_lock_for,但在 Xcode 中不支持
- boost::mutex 和 boost::timed_mutex 之间的区别
- Boost,指向互斥对象的指针,能起作用吗?boost::mutex和std::vector,不可复制的问题
- 在boost::mutex构造函数中同时写入同一内存区域可能存在错误
- 使用boost::mutex作为类的私有成员
- 可变boost::mutex可以分离锁和等待函数
- Boost::mutex, pthread_mutex_destroy失败的调试建议
- Boost::mutex - 是否可以将其锁定在一个类函数中并在另一个类函数上解锁
- replacing boost:: mutex