c++ - 如何在 C++ 中定义 vector <boost::mutex>?

标签 c++ boost vector mutex

我想用 boost::mutex 定义一个 vector ,例如:

  boost::mutex myMutex ;
  std::vector< boost::mutex > mutexVec; 
  mutexVec.push_back(myMutex); 

但是,我在 Linux 上遇到错误:

/boost_1_45_0v/include/boost/thread/pthread/mutex.hpp:33: 错误:“boost::mutex::mutex(const boost::mutex&)”是私有(private)的 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:104: 错误:在此上下文中

我在网上搜索找不到解决方案。

谢谢

最佳答案

你可以使用一个 boost pointer container :

#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 拥有其指针的所有权,以便在适当的时候删除它们,而不会产生智能指针可能引入的任何开销。

关于c++ - 如何在 C++ 中定义 vector <boost::mutex>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8252909/

相关文章:

c++ - C++ 中的复合 std::istream

c++ - CMake 没有找到 Boost

c++ - 如何在 C++ 中填充 int 和 vector<int> 的映射?

C++(在基于数组的堆栈中弹出)我们是否删除该元素?

c++ - 从 C++ 程序激活 Windows 时出错

c++ - 使用堆调试 MS VC++ 2005

c++ - 我可以用 typedef 对类非静态成员方法做什么

c++ - 尝试编译 boost 和 openssl 时出现编译错误(使用 Websocket++ 或 CPPRestSDK 时)

java - 如何将 Vector 转换为 JSONArray?

C++:尝试理解通过引用传递 vector