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

时间:2018-01-28 17:23:55

标签: 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;
};

这&#34;工作&#34;很好,但是,这一次(!this-&gt; haveFinished)是丑陋的地狱!

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

出现问题,只要我将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))

我知道这在信息方面相对稀少,但我可以随意问我任何事情,如果没有必要,我不想发帖需要几个小时才能阅读。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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