test_8596.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2013 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_THREAD_VERSION 4
  6. #include <iostream>
  7. #include <functional>
  8. //#include <future>
  9. #include <boost/thread.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. int f()
  12. {
  13. return 42;
  14. }
  15. boost::packaged_task<int()>* schedule(boost::function<int ()> const& fn)
  16. {
  17. // Normally, the pointer to the packaged task is stored in a queue
  18. // for execution on a separate thread, and the schedule function
  19. // would return just a future<T>
  20. boost::function<int ()> copy(fn);
  21. boost::packaged_task<int()>* result = new boost::packaged_task<int()>(copy);
  22. return result;
  23. }
  24. struct MyFunc
  25. {
  26. MyFunc(MyFunc const&) = delete;
  27. MyFunc& operator=(MyFunc const&) = delete;
  28. MyFunc() {};
  29. MyFunc(MyFunc &&) {};
  30. MyFunc& operator=(MyFunc &&) { return *this;};
  31. void operator()()const {}
  32. };
  33. int main()
  34. {
  35. boost::packaged_task<int()>* p(schedule(f));
  36. (*p)();
  37. boost::future<int> fut = p->get_future();
  38. std::cout << "The answer to the ultimate question: " << fut.get() << std::endl;
  39. {
  40. boost::function<void()> f;
  41. MyFunc mf;
  42. boost::packaged_task<void()> t1(f);
  43. boost::packaged_task<void()> t2(boost::move(mf));
  44. }
  45. return 0;
  46. }