From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16835 invoked by alias); 16 May 2014 07:18:02 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 16704 invoked by uid 48); 16 May 2014 07:17:59 -0000 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/60966] std::call_once sometime hangs Date: Fri, 16 May 2014 07:18:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 4.8.2 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: redi at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-05/txt/msg01427.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60966 --- Comment #25 from Jonathan Wakely --- (In reply to Leon Timmermans from comment #24) > > get_future() is non-const, set_value() is non-const. > > I can see your point from a C++ point of view, but this doesn't make sense > from a usable threading point of view. IMHO, the whole point of abstractions > such as promises is to isolate the user from such issues. Within reason yes, but not entirely. You can't expect to safely assign to the same std::promise in two threads for example. Anyway, that's not the real issue here, the example can be fixed to avoid such race conditions but still demonstrate the problem: #include #include #include struct DummyTask { DummyTask(int id) : id_(id) {} int id_; std::promise pr; }; const int THREADS = 100; void run_task(DummyTask* task) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); task->pr.set_value(); } void wait_for_task(std::future fut) { fut.wait(); } int main() { std::vector tasks; std::vector threads; std::vector> futures; for (int i = 0; i < THREADS; ++i) { DummyTask* task = new DummyTask(i); tasks.push_back(task); futures.push_back(task->pr.get_future()); threads.emplace_back(run_task, task); } for (int i = 0; i < THREADS; ++i) { wait_for_task(std::move(futures[i])); // Because we returned from wait_for_task for this task, run_task is surely done. // No one else is referring to the task. So, even before threads[i]->join(), // it should be safe to delete it now. delete tasks[i]; // but here you get an invalid read! } for (int i = 0; i < THREADS; ++i) { threads[i].join(); } }