只要我的線程執行,就需要使用此Functor,所以我創建了一個shared_ptr並試圖將它傳遞給std :: thread 。我在這裏複製了代碼和錯誤列表。將std :: shared_ptr傳遞給std :: thread的函數對象
struct Functor
{
std::string greeting;
explicit Functor(std::string _greeting="Hello!"): greeting { _greeting } {}
void operator()()
{
std::cout << greeting << "\n";
}
};
auto main() ->int
{
std::shared_ptr<Functor> fp = std::make_shared<Functor>();
std::thread t(&fp);
t.join();
return 0;
}
錯誤列表:
Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' std_threads C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr\xthread 240
Error C2672 'std::invoke': no matching overloaded function found std_threads C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr\xthread 240
我是新的C++ 11和併發性。請幫我理解以下內容
1> std :: thread是否在值傳遞時始終調用對象內的operator()?如果是這樣,爲什麼這樣定義。
2>如何確保給線程的資源一直保持在線程中?
3>是函子這裏寫的函數對象?
4>在這段代碼中我做了什麼?
應該不會是'的std ::線程t(* FP) 「而不是? – vu1p3n0x