작업의 경우 : 내가 위에 나열된 두 경우 모두를 시도하지 않은 그들은 문제없이 컴파일 더 mutable
왜 const 객체 안에 정의 된 뮤텍스를 잠글 수 있습니까?
template<typename T>
class threadsafe_queue
{
private:
std::mutex mut;
std::queue<T> data_queue;
public:
threadsafe_queue()
{}
threadsafe_queue(const threadsafe_queue& other)
{
std::lock_guard<std::mutex> lk(other.mut);
data_queue=other.data_queue;
}
};
std::mutex mut;
에 대한 참고 사항 : 실패
template<typename T>
class threadsafe_queue
{
private:
mutable std::mutex mut;
std::queue<T> data_queue;
public:
threadsafe_queue()
{}
threadsafe_queue(const threadsafe_queue& other)
{
std::lock_guard<std::mutex> lk(other.mut);
data_queue=other.data_queue;
}
};
케이스. 내부적으로 lock_guard가 mutex :: lock 함수를 호출한다고 가정합니다.이 함수 자체는 const 함수가 아닙니다.
질문 왜 복사 생성자에서 const 개체의 뮤텍스를 잠글 수 있습니까? 뮤텍스 mutable
될 자격이 때문에
확실하게 '변경 가능'이 핵심입니까? –
@OliverCharlesworth :'mutable' 키워드가 없어지는 두 번째 버전을보십시오. 질문자는 그 버전이 또한 작동한다고보고합니다. – user2357112
@ user2357112 - 실제로 작동하지 않습니다 : http://ideone.com/2MVs0O –