问题描述
c ++标准库中的许多类现在都有移动构造函数,例如 -
Many classes in the c++ standard library now have move constructors, for example -
thread::thread(thread&& t)
但是看起来std :: mutex没有。我理解他们不能被复制,但似乎有意义的是,能够从一个make_mutex函数返回一个例子。
But it appears that std::mutex does not. I understand that they can't be copied, but it seems to make sense to be able to return one from a "make_mutex" function for example. (Not saying it's useful, just that it makes sense)
有没有什么理由std :: mutex没有移动构造函数?
Is there any reason why std::mutex doesn't have a move constructor?
推荐答案
很好...主要是因为我不认为他们应该移动。直译。
Well... mainly because I don't think they should move. Literally.
在某些操作系统中,互斥量可能被建模为句柄(所以你可以复制它们),但是IIRC一个pthreads mutex被原地操作。如果你要重新定位,任何线程安全正在飞出窗口(其他线程如何知道互斥体刚刚改变了它的内存地址...)?
In some OS-es a mutex might be modeled as a handle (so you could copy them) but IIRC a pthreads mutex is manipulated in-place. If you are going to relocate that, any threadsafety is going fly right out of the window (how would the other threads know that the mutex had just changed it's memory address...)?
这篇关于移动std :: mutex的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!