| 314 | } |
| 315 | |
| 316 | void future_test::test_shared_future() |
| 317 | { |
| 318 | auto [p1, f1, s1] = prepare<int>(); |
| 319 | auto sf1 = f1.share(); |
| 320 | |
| 321 | test("future invalid after share", !f1.valid()); |
| 322 | test_eq("shared state moved on share", sf1.__state(), s1); |
| 323 | test_eq("no refcount increment on share", s1->refs(), 2); |
| 324 | |
| 325 | { |
| 326 | auto sf2 = sf1; |
| 327 | test_eq("refcount increment on copy", s1->refs(), 3); |
| 328 | test_eq("shared state shared between copies", sf1.__state(), sf2.__state()); |
| 329 | } |
| 330 | test_eq("refcount decrement when copy gets destroyed", s1->refs(), 2); |
| 331 | |
| 332 | auto sf2 = sf1; |
| 333 | int res1{}, res2{}; |
| 334 | std::thread t1{ |
| 335 | [&](){ |
| 336 | res1 = sf1.get(); |
| 337 | } |
| 338 | }; |
| 339 | std::thread t2{ |
| 340 | [&](){ |
| 341 | res2 = sf2.get(); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | std::this_thread::sleep_for(20ms); |
| 346 | p1.set_value(42); |
| 347 | std::this_thread::sleep_for(20ms); |
| 348 | test_eq("first result correct", res1, 42); |
| 349 | test_eq("second result correct", res2, 42); |
| 350 | } |
| 351 | } |