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

标签 c++ multithreading mutex

我目前正在开发一个项目,由于内部原因,该项目必须将其加载部分和执行部分放在单独的持久线程中。

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

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/

相关文章:

android - 在 Android 中使用计时器跳出无限循环

python - 将图像从 CV_64F 转换为 CV_8U

c++ - 初始化非默认可构造对象的动态数组

java - 为什么将 "this"传递给 Thread 构造函数?

c++ - Live555 - 使用 watchVariable 正确关闭客户端

c++ - 当 pthreads 在 mutex_lock/cond_wait 中等待时会发生什么?

c++ - 循环中的 Poco ScopedLock

c - 在 C 中监视同步线程的源代码

c++ - 我可以访问地址零吗?

c++ - 在结构中释放 gsl vector