Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

multithreading - Why is this not compiling? (RValue as thread CTOR arguments)

Hello here is a test code I wrote on MSVC12.

Could someone tell me why the std::move when I pass parameters to the thread are not converting the variabes to RValue refs?? And what I should do.

Thank you!

///some arbitrary long task
std::string DumpFile(std::string path){
  std::this_thread::sleep_for(std::chrono::seconds(10));
  return path;
}


void run_promise(std::promise<std::string> &&_prom, std::string &&_path){
  try {
    std::string val = DumpFile(std::move(_path));
    _prom.set_value(val);
  }
  catch (...) {
    _prom.set_exception(std::current_exception());
  }
}

std::future<std::string> ADumpFile(std::string && path) {
  std::promise<std::string> prms;
  std::future<std::string> fut = prms.get_future();
  std::thread th(run_promise, std::move(prms), std::move(path));
  th.detach();
  return fut;
}

int main(int argc, char* argv []){
  auto fut = ADumpFile("toto");
  while (fut.wait_for(std::chrono::seconds(1))!=std::future_status::ready){
    std::cout << "waiting
";
  }
  auto res = fut.get();
  getchar();
  return 0;
}

The error I get is :

Error 1 error C2664: 'void (std::promise<std::string> &&,std::string &&)' : cannot convert argument 2 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::string &&' C:Program Files (x86)Microsoft Visual Studio 12.0VCincludefunctional 1149 1 demo11 Error 2 error C2664: 'void (std::promise<std::string> &&,std::string &&)' : cannot convert argument 1 from 'std::promise<std::string>' to 'std::promise<std::string> &&' C:Program Files (x86)Microsoft Visual Studio 12.0VCincludefunctional 1149 1 demo11

Although, if I call run_promise(std::move(prom),std::move(path)) I have no problem.

Is this an issue related to passing the rvalue arguments through the thread CTOR?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, this problem was reported for VC11 http://connect.microsoft.com/VisualStudio/feedback/details/737812 and seems MS didn't address in VC12.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...