From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17022 invoked by alias); 15 May 2014 03:13:33 -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 16957 invoked by uid 48); 15 May 2014 03:13:22 -0000 From: "hideaki.kimura at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/60966] std::call_once sometime hangs Date: Thu, 15 May 2014 03:13: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: hideaki.kimura at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned 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/msg01342.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60966 --- Comment #14 from Hideaki Kimura --- (In reply to Jonathan Wakely from comment #13) > This means you are waiting on an object that has gone out of scope. WIthout > more information it's not possible to tell if this is a bug in your program > or the standard libary. > > I'll try to reproduce it with Thomas's code... Hey Jonathan, I'm not sure if this helps, but could you try the following code snippet? #include #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(DummyTask* task) { task->pr.get_future().wait(); } int main() { std::vector tasks; std::vector threads; for (int i = 0; i < THREADS; ++i) { DummyTask* task = new DummyTask(i); tasks.push_back(task); threads.push_back(new std::thread(run_task, task)); } for (int i = 0; i < THREADS; ++i) { wait_for_task(tasks[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(); delete threads[i]; } return 0; } Run it a few times on valgrind. You will see what I got. If you move threads[i]->join() to the line before delete tasks[i], you don't get the issue. [Assuming that I'm not terribly missing something..] My bet is that std::promise puts something on thread-local, which causes some issue when std::promise's destructor is called before the corresponding thread's join() is called.