async_pass.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/future.hpp>
  14. // template <class F, class... Args>
  15. // future<typename result_of<F(Args...)>::type>
  16. // async(F&& f, Args&&... args);
  17. // template <class F, class... Args>
  18. // future<typename result_of<F(Args...)>::type>
  19. // async(launch policy, F&& f, Args&&... args);
  20. //#define BOOST_THREAD_VERSION 3
  21. #define BOOST_THREAD_VERSION 4
  22. #include <boost/config.hpp>
  23. #if ! defined BOOST_NO_CXX11_DECLTYPE
  24. #define BOOST_RESULT_OF_USE_DECLTYPE
  25. #endif
  26. #include <iostream>
  27. #include <boost/thread/future.hpp>
  28. #include <boost/thread/thread.hpp>
  29. #include <boost/thread/detail/memory.hpp>
  30. #include <boost/thread/csbl/memory/unique_ptr.hpp>
  31. #include <memory>
  32. #include <boost/detail/lightweight_test.hpp>
  33. typedef boost::chrono::high_resolution_clock Clock;
  34. typedef boost::chrono::milliseconds ms;
  35. class A
  36. {
  37. long data_;
  38. public:
  39. typedef long result_type;
  40. explicit A(long i) :
  41. data_(i)
  42. {
  43. }
  44. long doit() const
  45. {
  46. boost::this_thread::sleep_for(ms(200));
  47. return data_;
  48. }
  49. long operator()() const
  50. {
  51. boost::this_thread::sleep_for(ms(200));
  52. return data_;
  53. }
  54. };
  55. class MoveOnly
  56. {
  57. public:
  58. typedef int result_type;
  59. int value;
  60. BOOST_THREAD_MOVABLE_ONLY(MoveOnly)
  61. MoveOnly()
  62. {
  63. value = 0;
  64. }
  65. MoveOnly( BOOST_THREAD_RV_REF(MoveOnly))
  66. {
  67. value = 1;
  68. }
  69. MoveOnly& operator=(BOOST_THREAD_RV_REF(MoveOnly))
  70. {
  71. value = 2;
  72. return *this;
  73. }
  74. int operator()() const
  75. {
  76. boost::this_thread::sleep_for(ms(200));
  77. return 3;
  78. }
  79. template <typename OS>
  80. friend OS& operator<<(OS& os, MoveOnly const& v)
  81. {
  82. os << v.value;
  83. return os;
  84. }
  85. };
  86. namespace boost
  87. {
  88. BOOST_THREAD_DCL_MOVABLE (MoveOnly)
  89. }
  90. int f0()
  91. {
  92. boost::this_thread::sleep_for(ms(200));
  93. return 3;
  94. }
  95. int i = 0;
  96. int& f1()
  97. {
  98. boost::this_thread::sleep_for(ms(200));
  99. return i;
  100. }
  101. void f2()
  102. {
  103. boost::this_thread::sleep_for(ms(200));
  104. }
  105. boost::csbl::unique_ptr<int> f3_0()
  106. {
  107. boost::this_thread::sleep_for(ms(200));
  108. boost::csbl::unique_ptr<int> r( (new int(3)));
  109. return boost::move(r);
  110. }
  111. MoveOnly f3_1()
  112. {
  113. boost::this_thread::sleep_for(ms(200));
  114. MoveOnly r;
  115. return boost::move(r);
  116. }
  117. boost::csbl::unique_ptr<int> f3(int i)
  118. {
  119. boost::this_thread::sleep_for(ms(200));
  120. return boost::csbl::unique_ptr<int>(new int(i));
  121. }
  122. boost::csbl::unique_ptr<int> f4(
  123. BOOST_THREAD_RV_REF_BEG boost::csbl::unique_ptr<int> BOOST_THREAD_RV_REF_END p
  124. )
  125. {
  126. boost::this_thread::sleep_for(ms(200));
  127. return boost::move(p);
  128. }
  129. struct check_timer {
  130. boost::chrono::nanoseconds delay;
  131. Clock::time_point start;
  132. check_timer(boost::chrono::nanoseconds delay)
  133. : delay(delay)
  134. , start(Clock::now())
  135. {
  136. }
  137. ~check_timer() {
  138. Clock::time_point now = Clock::now();
  139. BOOST_TEST(now - start < delay);
  140. std::cout << __FILE__ << "[" << __LINE__ << "] " << (now - start).count() << std::endl;
  141. }
  142. };
  143. int main()
  144. {
  145. {
  146. try {
  147. boost::async(f0);
  148. } catch (std::exception& ex)
  149. {
  150. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  151. BOOST_TEST(false && "exception thrown");
  152. }
  153. catch (...)
  154. {
  155. BOOST_TEST(false && "exception thrown");
  156. }
  157. }
  158. {
  159. try {
  160. boost::async(boost::launch::async, f0);
  161. } catch (std::exception& ex)
  162. {
  163. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  164. BOOST_TEST(false && "exception thrown");
  165. }
  166. catch (...)
  167. {
  168. BOOST_TEST(false && "exception thrown");
  169. }
  170. }
  171. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  172. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  173. {
  174. try {
  175. boost::async(boost::launch::deferred, f0);
  176. } catch (std::exception& ex)
  177. {
  178. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  179. BOOST_TEST(false && "exception thrown");
  180. }
  181. catch (...)
  182. {
  183. BOOST_TEST(false && "exception thrown");
  184. }
  185. }
  186. #endif
  187. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  188. {
  189. try
  190. {
  191. boost::future<int> f = boost::async(f0);
  192. boost::this_thread::sleep_for(ms(300));
  193. int res;
  194. {
  195. check_timer timer(ms(500));
  196. res = f.get();
  197. }
  198. BOOST_TEST(res == 3);
  199. }
  200. catch (std::exception& ex)
  201. {
  202. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  203. BOOST_TEST(false && "exception thrown");
  204. }
  205. catch (...)
  206. {
  207. BOOST_TEST(false && "exception thrown");
  208. }
  209. }
  210. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  211. {
  212. try
  213. {
  214. boost::shared_future<int> f = boost::async(f0).share();
  215. boost::this_thread::sleep_for(ms(300));
  216. int res;
  217. {
  218. check_timer timer(ms(500));
  219. res = f.get();
  220. }
  221. BOOST_TEST(res == 3);
  222. }
  223. catch (std::exception& ex)
  224. {
  225. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  226. BOOST_TEST(false && "exception thrown");
  227. }
  228. catch (...)
  229. {
  230. BOOST_TEST(false && "exception thrown");
  231. }
  232. }
  233. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  234. {
  235. try
  236. {
  237. boost::future<int> f = boost::async(boost::launch::async, f0);
  238. boost::this_thread::sleep_for(ms(300));
  239. int res;
  240. {
  241. check_timer timer(ms(500));
  242. res = f.get();
  243. }
  244. BOOST_TEST(res == 3);
  245. }
  246. catch (std::exception& ex)
  247. {
  248. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  249. BOOST_TEST(false && "exception thrown");
  250. }
  251. catch (...)
  252. {
  253. BOOST_TEST(false && "exception thrown");
  254. }
  255. }
  256. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  257. {
  258. try
  259. {
  260. boost::future<long> f = boost::async(boost::launch::async, A(3));
  261. boost::this_thread::sleep_for(ms(300));
  262. int res;
  263. {
  264. check_timer timer(ms(500));
  265. res = f.get();
  266. }
  267. BOOST_TEST(res == 3);
  268. }
  269. catch (std::exception& ex)
  270. {
  271. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  272. BOOST_TEST(false && "exception thrown");
  273. }
  274. catch (...)
  275. {
  276. BOOST_TEST(false && "exception thrown");
  277. }
  278. }
  279. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  280. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  281. {
  282. try
  283. {
  284. boost::future<long> f = boost::async(boost::launch::deferred, A(3));
  285. //boost::this_thread::sleep_for(ms(300));
  286. int res;
  287. {
  288. check_timer timer(ms(500));
  289. res = f.get();
  290. }
  291. BOOST_TEST(res == 3);
  292. }
  293. catch (std::exception& ex)
  294. {
  295. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  296. BOOST_TEST(false && "exception thrown");
  297. }
  298. catch (...)
  299. {
  300. BOOST_TEST(false && "exception thrown");
  301. }
  302. }
  303. #endif
  304. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  305. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  306. {
  307. try
  308. {
  309. A a(3);
  310. boost::future<long> f = boost::async(boost::launch::async, &A::doit, &a);
  311. boost::this_thread::sleep_for(ms(300));
  312. int res;
  313. {
  314. check_timer timer(ms(500));
  315. res = f.get();
  316. }
  317. BOOST_TEST(res == 3);
  318. }
  319. catch (std::exception& ex)
  320. {
  321. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  322. BOOST_TEST(false && "exception thrown");
  323. }
  324. catch (...)
  325. {
  326. BOOST_TEST(false && "exception thrown");
  327. }
  328. }
  329. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  330. {
  331. try
  332. {
  333. A a(3);
  334. boost::future<long> f = boost::async(boost::launch::deferred, &A::doit, &a);
  335. boost::this_thread::sleep_for(ms(300));
  336. int res;
  337. {
  338. check_timer timer(ms(500));
  339. res = f.get();
  340. }
  341. BOOST_TEST(res == 3);
  342. }
  343. catch (std::exception& ex)
  344. {
  345. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  346. BOOST_TEST(false && "exception thrown");
  347. }
  348. catch (...)
  349. {
  350. BOOST_TEST(false && "exception thrown");
  351. }
  352. }
  353. #endif
  354. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  355. {
  356. try
  357. {
  358. boost::future<int> f = boost::async(boost::launch::async, BOOST_THREAD_MAKE_RV_REF(MoveOnly()));
  359. boost::this_thread::sleep_for(ms(300));
  360. int res;
  361. {
  362. check_timer timer(ms(500));
  363. res = f.get();
  364. }
  365. BOOST_TEST(res == 3);
  366. }
  367. catch (std::exception& ex)
  368. {
  369. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  370. BOOST_TEST(false && "exception thrown");
  371. }
  372. catch (...)
  373. {
  374. BOOST_TEST(false && "exception thrown");
  375. }
  376. }
  377. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  378. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  379. {
  380. try
  381. {
  382. boost::future<int> f = boost::async(boost::launch::deferred, BOOST_THREAD_MAKE_RV_REF(MoveOnly()));
  383. boost::this_thread::sleep_for(ms(300));
  384. int res;
  385. {
  386. check_timer timer(ms(500));
  387. res = f.get();
  388. }
  389. BOOST_TEST(res == 3);
  390. }
  391. catch (std::exception& ex)
  392. {
  393. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  394. BOOST_TEST(false && "exception thrown");
  395. }
  396. catch (...)
  397. {
  398. BOOST_TEST(false && "exception thrown");
  399. }
  400. }
  401. #endif
  402. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  403. {
  404. try
  405. {
  406. boost::future<int> f = boost::async(boost::launch::any, f0);
  407. boost::this_thread::sleep_for(ms(300));
  408. int res;
  409. {
  410. check_timer timer(ms(500));
  411. res = f.get();
  412. }
  413. BOOST_TEST(res == 3);
  414. }
  415. catch (std::exception& ex)
  416. {
  417. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  418. BOOST_TEST(false && "exception thrown");
  419. }
  420. catch (...)
  421. {
  422. BOOST_TEST(false && "exception thrown");
  423. }
  424. }
  425. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  426. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  427. {
  428. try
  429. {
  430. boost::future<int> f = boost::async(boost::launch::deferred, f0);
  431. //boost::this_thread::sleep_for(ms(300));
  432. int res;
  433. {
  434. check_timer timer(ms(500));
  435. res = f.get();
  436. }
  437. BOOST_TEST(res == 3);
  438. }
  439. catch (std::exception& ex)
  440. {
  441. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  442. BOOST_TEST(false && "exception thrown");
  443. }
  444. catch (...)
  445. {
  446. BOOST_TEST(false && "exception thrown");
  447. }
  448. }
  449. #endif
  450. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  451. {
  452. try
  453. {
  454. boost::future<int&> f = boost::async(f1);
  455. boost::this_thread::sleep_for(ms(300));
  456. int* res;
  457. {
  458. check_timer timer(ms(500));
  459. res = &f.get();
  460. }
  461. BOOST_TEST(res == &i);
  462. }
  463. catch (std::exception& ex)
  464. {
  465. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  466. BOOST_TEST(false && "exception thrown");
  467. }
  468. catch (...)
  469. {
  470. BOOST_TEST(false && "exception thrown");
  471. }
  472. }
  473. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  474. {
  475. try
  476. {
  477. boost::future<int&> f = boost::async(boost::launch::async, f1);
  478. boost::this_thread::sleep_for(ms(300));
  479. int* res;
  480. {
  481. check_timer timer(ms(500));
  482. res = &f.get();
  483. }
  484. BOOST_TEST(res == &i);
  485. }
  486. catch (std::exception& ex)
  487. {
  488. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  489. BOOST_TEST(false && "exception thrown");
  490. }
  491. catch (...)
  492. {
  493. BOOST_TEST(false && "exception thrown");
  494. }
  495. }
  496. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  497. {
  498. try
  499. {
  500. boost::future<int&> f = boost::async(boost::launch::any, f1);
  501. boost::this_thread::sleep_for(ms(300));
  502. int* res;
  503. {
  504. check_timer timer(ms(500));
  505. res = &f.get();
  506. }
  507. BOOST_TEST(res == &i);
  508. }
  509. catch (std::exception& ex)
  510. {
  511. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  512. BOOST_TEST(false && "exception thrown");
  513. }
  514. catch (...)
  515. {
  516. BOOST_TEST(false && "exception thrown");
  517. }
  518. }
  519. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  520. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  521. {
  522. try
  523. {
  524. boost::future<int&> f = boost::async(boost::launch::deferred, f1);
  525. //boost::this_thread::sleep_for(ms(300));
  526. int* res;
  527. {
  528. check_timer timer(ms(500));
  529. res = &f.get();
  530. }
  531. BOOST_TEST(res == &i);
  532. }
  533. catch (std::exception& ex)
  534. {
  535. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  536. BOOST_TEST(false && "exception thrown");
  537. }
  538. catch (...)
  539. {
  540. BOOST_TEST(false && "exception thrown");
  541. }
  542. }
  543. #endif
  544. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  545. {
  546. try
  547. {
  548. boost::future<void> f = boost::async(f2);
  549. boost::this_thread::sleep_for(ms(300));
  550. {
  551. check_timer timer(ms(500));
  552. f.get();
  553. }
  554. }
  555. catch (std::exception& ex)
  556. {
  557. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  558. BOOST_TEST(false && "exception thrown");
  559. }
  560. catch (...)
  561. {
  562. BOOST_TEST(false && "exception thrown");
  563. }
  564. }
  565. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  566. {
  567. try
  568. {
  569. boost::future<void> f = boost::async(boost::launch::async, f2);
  570. boost::this_thread::sleep_for(ms(300));
  571. {
  572. check_timer timer(ms(500));
  573. f.get();
  574. }
  575. }
  576. catch (std::exception& ex)
  577. {
  578. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  579. BOOST_TEST(false && "exception thrown");
  580. }
  581. catch (...)
  582. {
  583. BOOST_TEST(false && "exception thrown");
  584. }
  585. }
  586. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  587. {
  588. try
  589. {
  590. boost::future<void> f = boost::async(boost::launch::any, f2);
  591. boost::this_thread::sleep_for(ms(300));
  592. {
  593. check_timer timer(ms(500));
  594. f.get();
  595. }
  596. }
  597. catch (std::exception& ex)
  598. {
  599. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  600. BOOST_TEST(false && "exception thrown");
  601. }
  602. catch (...)
  603. {
  604. BOOST_TEST(false && "exception thrown");
  605. }
  606. }
  607. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  608. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  609. {
  610. try
  611. {
  612. boost::future<void> f = boost::async(boost::launch::deferred, f2);
  613. //boost::this_thread::sleep_for(ms(300));
  614. {
  615. check_timer timer(ms(500));
  616. f.get();
  617. }
  618. }
  619. catch (std::exception& ex)
  620. {
  621. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  622. BOOST_TEST(false && "exception thrown");
  623. }
  624. catch (...)
  625. {
  626. BOOST_TEST(false && "exception thrown");
  627. }
  628. }
  629. #endif
  630. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  631. {
  632. try
  633. {
  634. boost::future<MoveOnly> f = boost::async(&f3_1);
  635. boost::this_thread::sleep_for(ms(300));
  636. MoveOnly res;
  637. {
  638. check_timer timer(ms(500));
  639. res = f.get();
  640. }
  641. BOOST_TEST_EQ(res.value, 2);
  642. }
  643. catch (std::exception& ex)
  644. {
  645. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  646. BOOST_TEST(false && "exception thrown");
  647. }
  648. catch (...)
  649. {
  650. BOOST_TEST(false && "exception thrown");
  651. }
  652. }
  653. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  654. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  655. {
  656. try
  657. {
  658. boost::future<MoveOnly> f = boost::async(boost::launch::deferred, &f3_1);
  659. //boost::this_thread::sleep_for(ms(300));
  660. MoveOnly res;
  661. {
  662. check_timer timer(ms(500));
  663. res = f.get();
  664. }
  665. BOOST_TEST_EQ(res.value, 2);
  666. }
  667. catch (std::exception& ex)
  668. {
  669. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  670. BOOST_TEST(false && "exception thrown");
  671. }
  672. catch (...)
  673. {
  674. BOOST_TEST(false && "exception thrown");
  675. }
  676. }
  677. #endif
  678. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  679. {
  680. try
  681. {
  682. boost::future<MoveOnly> f;
  683. f = boost::async(&f3_1);
  684. boost::this_thread::sleep_for(ms(300));
  685. MoveOnly res;
  686. {
  687. check_timer timer(ms(500));
  688. res = f.get();
  689. }
  690. BOOST_TEST(res.value == 2);
  691. }
  692. catch (std::exception& ex)
  693. {
  694. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  695. BOOST_TEST(false && "exception thrown");
  696. }
  697. catch (...)
  698. {
  699. BOOST_TEST(false && "exception thrown");
  700. }
  701. }
  702. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  703. {
  704. try
  705. {
  706. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(&f3_0);
  707. boost::this_thread::sleep_for(ms(300));
  708. boost::csbl::unique_ptr<int> res;
  709. {
  710. check_timer timer(ms(500));
  711. res = f.get();
  712. }
  713. BOOST_TEST(*res == 3);
  714. }
  715. catch (std::exception& ex)
  716. {
  717. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  718. BOOST_TEST(false && "exception thrown");
  719. }
  720. catch (...)
  721. {
  722. BOOST_TEST(false && "exception thrown");
  723. }
  724. }
  725. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  726. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  727. {
  728. try
  729. {
  730. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(boost::launch::async, &f3, 3);
  731. boost::this_thread::sleep_for(ms(300));
  732. boost::csbl::unique_ptr<int> res;
  733. {
  734. check_timer timer(ms(500));
  735. res = f.get();
  736. }
  737. BOOST_TEST(*res == 3);
  738. }
  739. catch (std::exception& ex)
  740. {
  741. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  742. BOOST_TEST(false && "exception thrown");
  743. }
  744. catch (...)
  745. {
  746. BOOST_TEST(false && "exception thrown");
  747. }
  748. }
  749. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  750. {
  751. try
  752. {
  753. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(boost::launch::deferred, &f3, 3);
  754. //boost::this_thread::sleep_for(ms(300));
  755. boost::csbl::unique_ptr<int> res;
  756. {
  757. check_timer timer(ms(500));
  758. res = f.get();
  759. }
  760. BOOST_TEST(*res == 3);
  761. }
  762. catch (std::exception& ex)
  763. {
  764. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  765. BOOST_TEST(false && "exception thrown");
  766. }
  767. catch (...)
  768. {
  769. BOOST_TEST(false && "exception thrown");
  770. }
  771. }
  772. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  773. {
  774. try
  775. {
  776. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(&f3, 3);
  777. boost::this_thread::sleep_for(ms(300));
  778. boost::csbl::unique_ptr<int> res;
  779. {
  780. check_timer timer(ms(500));
  781. res = f.get();
  782. }
  783. BOOST_TEST(*res == 3);
  784. }
  785. catch (std::exception& ex)
  786. {
  787. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  788. BOOST_TEST(false && "exception thrown");
  789. }
  790. catch (...)
  791. {
  792. BOOST_TEST(false && "exception thrown");
  793. }
  794. }
  795. #endif
  796. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  797. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  798. {
  799. try
  800. {
  801. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(boost::launch::async, &f4, boost::csbl::unique_ptr<int>(new int(3)));
  802. boost::this_thread::sleep_for(ms(300));
  803. boost::csbl::unique_ptr<int> res;
  804. {
  805. check_timer timer(ms(500));
  806. res = f.get();
  807. }
  808. BOOST_TEST(*res == 3);
  809. }
  810. catch (std::exception& ex)
  811. {
  812. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  813. BOOST_TEST(false && "exception thrown");
  814. }
  815. catch (...)
  816. {
  817. BOOST_TEST(false && "exception thrown");
  818. }
  819. }
  820. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  821. {
  822. try
  823. {
  824. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(boost::launch::deferred, &f4, boost::csbl::unique_ptr<int>(new int(3)));
  825. //boost::this_thread::sleep_for(ms(300));
  826. boost::csbl::unique_ptr<int> res;
  827. {
  828. check_timer timer(ms(500));
  829. res = f.get();
  830. }
  831. BOOST_TEST(*res == 3);
  832. }
  833. catch (std::exception& ex)
  834. {
  835. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  836. BOOST_TEST(false && "exception thrown");
  837. }
  838. catch (...)
  839. {
  840. BOOST_TEST(false && "exception thrown");
  841. }
  842. }
  843. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<std::endl;
  844. {
  845. try
  846. {
  847. boost::future<boost::csbl::unique_ptr<int> > f = boost::async(&f4, boost::csbl::unique_ptr<int>(new int(3)));
  848. boost::this_thread::sleep_for(ms(300));
  849. boost::csbl::unique_ptr<int> res;
  850. {
  851. check_timer timer(ms(500));
  852. res = f.get();
  853. }
  854. BOOST_TEST(*res == 3);
  855. }
  856. catch (std::exception& ex)
  857. {
  858. std::cout << __FILE__ <<"["<<__LINE__<<"]"<<ex.what() << std::endl;
  859. BOOST_TEST(false && "exception thrown");
  860. }
  861. catch (...)
  862. {
  863. BOOST_TEST(false && "exception thrown");
  864. }
  865. }
  866. #endif
  867. return boost::report_errors();
  868. }