gpt4 book ai didi

c++ - 导致编译错误的模板化类成员互斥锁c++

转载 作者:行者123 更新时间:2023-11-30 05:05:16 25 4
gpt4 key购买 nike

我目前正在从事一个项目,出于内部原因,该项目必须在单独的持久线程中具有加载部分和执行部分。

我制作了一个工具来处理执行线程和加载线程之间的加载请求。这个工具的基本逻辑就搞定了:

template <typename T>
class LoadingHandler {
public:
//std::mutex m_mutex;
LoadingHandler() : haveFinished(false) {
}
const T &getResult() const {
if(!this->haveFinished){
std::cout << "asking thread is going to sleep" << std::endl;
while(!this->haveFinished){

}
}
std::cout << "asking thread woke up" << std::endl;
return this->resultValue;
}
void setResult(const T &resultValue){
std::cout << "Loading thread have finished! " << std::endl;
this->resultValue = resultValue;
this->haveFinished = true;
std::cout << "handler result is : " << this->resultValue << std::endl;
}
private:
T resultValue;
bool haveFinished;
};

这个“工作”很好,但是,这个 while(!this->haveFinished) 丑得要命!

解决方案?也许是一些互斥量和条件变量!

问题来了,只要我向此类添加一个 std::mutex 成员(或在我的示例中取消注释该行),编译器就会告诉我各种我不明白的事情,这里是第一行:

g++ -std=c++11 -Wall -c -o build/main.o -L/usr/X11R6/lib sources/main.cpp -lglut -lGL -lGLU -lGLEW -lm -pthread -lglfw
In file included from /usr/include/c++/5/functional:55:0,
from /usr/include/c++/5/thread:39,
from sources/../headers/Tools/LoadingHandler.hpp:9,
from sources/main.cpp:6:
/usr/include/c++/5/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = LoadingHandler<int>; long unsigned int _Idx = 0ul; _Head = LoadingHandler<int>]’:
/usr/include/c++/5/tuple:369:49: required from ‘constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head>&&) [with long unsigned int _Idx = 0ul; _Head = LoadingHandler<int>]’
/usr/include/c++/5/tuple:484:17: required from ‘std::_Bind<_Functor(_Bound_args ...)>::_Bind(std::_Bind<_Functor(_Bound_args ...)>&&) [with _Functor = void (*)(LoadingHandler<int>&); _Bound_args = {LoadingHandler<int>}]’
sources/main.cpp:36:84: required from here
/usr/include/c++/5/tuple:115:42: error: use of deleted function ‘LoadingHandler<int>::LoadingHandler(LoadingHandler<int>&&)’
: _M_head_impl(std::forward<_UHead>(__h)) { }
^
In file included from sources/main.cpp:6:0:
sources/../headers/Tools/LoadingHandler.hpp:15:7: note: ‘LoadingHandler<int>::LoadingHandler(LoadingHandler<int>&&)’ is implicitly deleted because the default definition would be ill-formed:
class LoadingHandler {
^
sources/../headers/Tools/LoadingHandler.hpp:15:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from sources/../headers/Tools/LoadingHandler.hpp:11:0,
from sources/main.cpp:6:
/usr/include/c++/5/mutex:129:5: note: declared here
mutex(const mutex&) = delete;
^
In file included from /usr/include/c++/5/thread:39:0,
from sources/../headers/Tools/LoadingHandler.hpp:9,
from sources/main.cpp:6:
/usr/include/c++/5/functional: In instantiation of ‘std::_Bind<_Functor(_Bound_args ...)>::_Bind(std::_Bind<_Functor(_Bound_args ...)>&&) [with _Functor = void (*)(LoadingHandler<int>&); _Bound_args = {LoadingHandler<int>}]’:
sources/main.cpp:36:84: required from here
/usr/include/c++/5/functional:1120:78: note: synthesized method ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(std::tuple< <template-parameter-1-1> >&&) [with _Elements = {LoadingHandler<int>}]’ first required here
: _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))

我知道这方面的信息相对较少,但请随时问我任何问题,如果没有必要,我不想发表需要花费数小时才能阅读的帖子。

感谢您的帮助!

最佳答案

std::mutex 不可移动(或复制),因此如果要移动或复制包含 std::mutex 成员的类,您必须提供您自己的移动/复制构造函数和移动/复制赋值运算符。

关于c++ - 导致编译错误的模板化类成员互斥锁c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48489223/

25 4 0