Rebase.
[official-gcc.git] / libstdc++-v3 / testsuite / 30_threads / promise / 60966.cc
blob47333a1fd0ebaeb9a673eef57015d278d5f68899
1 // { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } }
2 // { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } }
3 // { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } }
4 // { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-darwin* } }
5 // { dg-require-cstdint "" }
6 // { dg-require-gthreads "" }
7 // { dg-require-atomic-builtins "" }
9 // Copyright (C) 2014 Free Software Foundation, Inc.
11 // This file is part of the GNU ISO C++ Library. This library is free
12 // software; you can redistribute it and/or modify it under the
13 // terms of the GNU General Public License as published by the
14 // Free Software Foundation; either version 3, or (at your option)
15 // any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
22 // You should have received a copy of the GNU General Public License along
23 // with this library; see the file COPYING3. If not see
24 // <http://www.gnu.org/licenses/>.
26 // libstdc++/60966
27 // This test hangs if std::promise::~promise() destroys the
28 // shared state before std::promise::set_value() finishes using it.
30 #include <future>
31 #include <thread>
32 #include <vector>
34 const int THREADS = 10;
36 void run_task(std::promise<void>* pr)
38 std::this_thread::sleep_for(std::chrono::milliseconds(100));
39 pr->set_value();
42 int main()
44 std::vector<std::promise<void>*> tasks(THREADS);
45 std::vector<std::thread> threads(THREADS);
46 std::vector<std::future<void>> futures(THREADS);
48 for (int i = 0; i < THREADS; ++i)
50 std::promise<void>* task = new std::promise<void>;
51 tasks[i] = task;
52 futures[i] = task->get_future();
53 threads[i] = std::thread(run_task, task);
56 for (int i = 0; i < THREADS; ++i)
58 // the temporary future releases the state as soon as wait() returns
59 std::future<void>(std::move(futures[i])).wait();
60 // state is ready, should now be safe to delete promise, so it
61 // releases the shared state too
62 delete tasks[i];
65 for (auto& t : threads)
66 t.join();