| 1 | // { dg-do run { target c++17 } } |
| 2 | // { dg-additional-options "-pthread" { target pthread } } |
| 3 | // { dg-require-gthreads "" } |
| 4 | |
| 5 | #include <experimental/synchronized_value> |
| 6 | #include <testsuite_hooks.h> |
| 7 | #include <string> |
| 8 | |
| 9 | using std::experimental::synchronized_value; |
| 10 | |
| 11 | synchronized_value<std::string> s; |
| 12 | |
| 13 | std::string read_value(){ |
| 14 | return apply([](auto& x){return x;},s); |
| 15 | } |
| 16 | |
| 17 | void set_value(std::string const& new_val){ |
| 18 | apply([&](auto& x){ x = new_val; }, s); |
| 19 | } |
| 20 | |
| 21 | void |
| 22 | test_single() |
| 23 | { |
| 24 | set_value("new value"); |
| 25 | VERIFY( read_value() == "new value" ); |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | test_multi() |
| 30 | { |
| 31 | synchronized_value<int> a(1), b(2), c(3); |
| 32 | int sum = apply([](auto&... ints) { return (ints++ + ...); }, a, b, c); |
| 33 | VERIFY( sum == 6 ); |
| 34 | auto get = [](int& i) { return i; }; |
| 35 | VERIFY( apply(get, a) == 2 ); |
| 36 | VERIFY( apply(get, b) == 3 ); |
| 37 | VERIFY( apply(get, c) == 4 ); |
| 38 | } |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | test_single(); |
| 43 | test_multi(); |
| 44 | } |