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 "" } 8 9// Copyright (C) 2014-2015 Free Software Foundation, Inc. 10// 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. 16 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. 21 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/>. 25 26// libstdc++/60966 27// This test hangs if std::promise::~promise() destroys the 28// shared state before std::promise::set_value() finishes using it. 29 30#include <future> 31#include <thread> 32#include <vector> 33 34const int THREADS = 10; 35 36void run_task(std::promise<void>* pr) 37{ 38 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 39 pr->set_value(); 40} 41 42int main() 43{ 44 std::vector<std::promise<void>*> tasks(THREADS); 45 std::vector<std::thread> threads(THREADS); 46 std::vector<std::future<void>> futures(THREADS); 47 48 for (int i = 0; i < THREADS; ++i) 49 { 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); 54 } 55 56 for (int i = 0; i < THREADS; ++i) 57 { 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]; 63 } 64 65 for (auto& t : threads) 66 t.join(); 67} 68