c++ - Vectors of lock_guards -
i'm working multithreaded code (working concurrent data structures), , part of requires lock set of mutexes. implementation utilizing vector of lock_guards, since don't know how many mutexes i'm going need lock, , may run exception conditions force me unlock of mutexes , restart. hence reason vector.
the code i'm trying use boils down this:
#include <mutex> #include <vector> using namespace std; int main( int argc, char** argv ) { vector<recursive_mutex> vec(10); vector<lock_guard<recursive_mutex>> lgv; for( auto = vec.begin(); != vec.end(); ++it ) { lgv.emplace_back( *it ); } return 0; } when try compile (g++ 5.3.1 using --std=c++11), following error (somewhat distilled):
in file included foo.cpp:1:0: /usr/include/c++/5.3.1/mutex:385:7: note: declared here lock_guard(const lock_guard&) = delete; based upon understanding of emplace_back, library should not attempting use copy constructor lock_guard -- should performing construct-in-place. understanding should happening properly, or understanding flawed?
fyi, have attempted use unique_lock , compile fine. however, i'm curious (apparent) discrepancy.
i assume, question reflects attemtpt use unique_lock, op saying works. same example lock_guard not work, since std::vector::emplace_back requires type both moveinsertable , emplaceconstructible, , std::lock_guard not suit.
Comments
Post a Comment