mardi 29 septembre 2015

C++ Passing a reference variable to the thread in g++-4.7.4

I need to start a thread passing complex parameters (std::thread<>) as a parameter when the thread starts. I´m using `std::ref. This code works fine with updated environments (g++-4.8.2 running on Ubuntu).

Now I have to compile this same code in a old compiler (g++4.7.4) and I´m getting errors.

The code is shown below, as well as the error:

ReaderThread.hpp

class ReaderThread {
    void start(Reader reader, SyncController &syncController);
}

ReaderThread.cpp

void ReaderThread::start(Reader reader, SyncController &syncController)
{

        Do something...
}

main.cpp

int main()
{

    ...do stuff...

    /* 
     * Create and start the reader thread. The created object must live
     * during the whole thread life.
     * std::ref is used to pass as reference
     */
    myReader = ReaderFactory(params);

    std::shared_ptr<ReaderThread> ptr(new ReaderThread); 
    std::thread th(&ReaderThread::start, ptr, myReader, std::ref(syncController));


    ...do other stuff...
}

ERROR:

In file included from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/move.h:57:0,
                 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_pair.h:61,
                 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_algobase.h:65,
                 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/char_traits.h:41,
                 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ios:41,
                 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ostream:40,
                 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/iostream:40,
                 from ./main.cpp:11:
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>, std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController> >':
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits:1857:12:   required from 'class std::result_of<std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>(std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController>)>'
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/functional:1563:61:   required from 'struct std::_Bind_simple<std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>(std::shared_ptr<aeirtuthread::ReaderThread>, Reader, std::reference_wrapper<SyncController>)>'
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/thread:133:9:   required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ReaderThread::*)(Reader, SyncController&); _Args = {std::shared_ptr<ReaderThread>&, Reader&, std::reference_wrapper<SyncController>}]'
./aeirtu/aeirtu/main.cpp:155:96:   required from here
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits:1834:9: error: no match for call to '(std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>) (std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController>)'

I can´t find out if this error is being caused by the use of std::ref on older compilers or from something different.

Help appreciated.

Aucun commentaire:

Enregistrer un commentaire