2015-09-29 44 views
0

我需要在線程啓動時啓動一個將複雜參數(std :: thread <>)作爲參數的線程。我正在使用`std :: ref。此代碼可以在更新的環境中正常工作(在Ubuntu上運行g ++ - 4.8.2)。C++將引用變量傳遞給g ++中的線程 - 4.7.4

現在我不得不在舊的編譯器(g ++ 4.7.4)中編譯相同的代碼,並且出現錯誤。

的代碼如下所示,以及該錯誤:

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... 
} 

錯誤:

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>)' 

我不能看看這個錯誤正在對舊的編譯器或不同的東西因使用的std::ref

幫助我們找到一個將被4.7.4支持的編譯器並編譯我的代碼。

+0

雖然C++ 11在gcc 4.8.x中幾乎完全支持,但gcc 4.7.x缺少一些功能。在這裏檢查:https://gcc.gnu.org/projects/cxx0x.html – SHR

+0

確實,但我需要修復代碼編譯...幫助讚賞... – Mendes

+0

使用指針,而不是參考。 –

回答

2

gcc 4.7似乎無法處理作爲對象提供的shared_ptr(或unique_ptr)。它工作正常,自然的指針,但 - 這樣一個可能的解決辦法是用以下取代線程的創建(如果合適的話,當然):

std::thread th(&ReaderThread::start, &myThread, myReader, std::ref(syncController)); 

現在,如果這不是需要分配的指針是可行的和真實的,以下是爲您的想法更換:

std::thread th(
       [ptr, &syncController, myReader]() { 
         ptr->start(myReader, syncController); 
       } 
       ); 

} 

在這個例子中,synController是按引用傳遞,一切按值,同樣的還有在您的文章。

+0

由一個自然指針取代,現在它工作正常...無論如何,我會嘗試第二個建議以及...謝謝。 – Mendes

+0

@Mendez,自然的指針,當然是最直接的。唯一需要注意的是,如果您需要管理對象的生命週期,那麼自然指針可能不會。 – SergeyA