Sunday, 15 March 2015

c++ - Why is std::packaged_task not valid? -



c++ - Why is std::packaged_task<void()> not valid? -

using msvc2012,

the next code compile , run expected

std::packaged_task< int() > task( []()->int{ std::cout << "hello world" << std::endl; homecoming 0; } ); std::thread t( std::move(task) ); t.join();

while next code fail compile , run

std::packaged_task< void() > task( [](){ std::cout << "hello world" << std::endl; } ); std::thread t( std::move(task) ); t.join();

why so?

edit: workaround, possible utilize std::promise std::future on function returns void

std::promise<void> promise; auto future = promise.get_future(); std::thread thread( [](std::promise<void> &p){ std::cout << "hello world" << std::endl; p.set_value(); }, std::move(promise) ); future.wait();

note there bug in vs2012 library std::thread forces pass promise in l-value reference , move promise in, not compile if pass promise value or r-value reference. supposedly because implementation uses std::bind() not behave expected.

this bug in msvc2012. there quite few bugs in thread library implementation ships msvc2012. posted partial list in blog post comparing commercial just::thread library: http://www.justsoftwaresolutions.co.uk/news/just-thread-v1.8.0-released.html

c++ c++11 packaged-task

No comments:

Post a Comment