From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3405 invoked by alias); 22 Aug 2012 20:29:09 -0000 Received: (qmail 3394 invoked by uid 22791); 22 Aug 2012 20:29:07 -0000 X-SWARE-Spam-Status: No, hits=-3.6 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 22 Aug 2012 20:28:54 +0000 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/54352] New: relaxed data race rules for ~condition_variable_any Date: Wed, 22 Aug 2012 20:29:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Keywords: ABI X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 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 X-SW-Source: 2012-08/txt/msg01565.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54352 Bug #: 54352 Summary: relaxed data race rules for ~condition_variable_any Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: ABI Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: redi@gcc.gnu.org (related to PR 54185, but for condition_variable_any) As Howard pointed out in c++std-lib-32966 this should work: #include #include #include #include #include #include #include #include template class locked_list { std::mutex mut_; std::list list_; public: typedef typename std::list::iterator iterator; typedef typename T::key key; template void emplace_back(Args&& ...args) {list_.emplace_back(std::forward(args)...);} iterator find(const key& k) { std::unique_lock lk(mut_); while (true) { iterator ep = std::find(list_.begin(), list_.end(), k); if (ep == list_.end()) return ep; if (!ep->busy()) { ep->set_busy(); return ep; } ep->wait(lk); } } void erase(iterator i) { std::lock_guard _(mut_); assert(i->busy()); i->notify_all(); list_.erase(i); } iterator end() {return list_.end();} }; template class elt { Key key_; std::condition_variable_any notbusy_; bool busy_; public: typedef Key key; explicit elt(const Key& k) : key_(k), busy_(false) {} bool busy() const {return busy_;} void set_busy() {busy_ = true;} void unset_busy() {busy_ = false;} template void wait(Lock& lk) {notbusy_.wait(lk);} void notify_all() {notbusy_.notify_all();} bool operator==(const Key& k) const {return key_ == k;} }; void f1(locked_list>& list) { auto i = list.find(1); assert(i != list.end()); std::this_thread::sleep_for(std::chrono::milliseconds(500)); list.erase(i); } void f2(locked_list>& list) { auto i = list.find(1); assert(i == list.end()); } int main() { locked_list> list; list.emplace_back(1); std::thread t1 = std::thread(f1, std::ref(list)); std::this_thread::sleep_for(std::chrono::milliseconds(250)); std::thread t2 = std::thread(f2, std::ref(list)); t1.join(); t2.join(); } This test doesn't actually crash with libstdc++, but valgrind shows it's faulty. Fixing this involves replacing std::condition_variable_any::_M_mutex with a std::shared_ptr