From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 38974 invoked by alias); 11 Mar 2015 19:00:31 -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 38924 invoked by uid 48); 11 Mar 2015 19:00:28 -0000 From: "klemensbaum at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency Date: Wed, 11 Mar 2015 19:00: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-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: klemensbaum 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter Message-ID: 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: 2015-03/txt/msg01252.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65393 Bug ID: 65393 Summary: std::thread shared_ptr inefficiency Product: gcc Version: unknown Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: klemensbaum at gmail dot com The std::thread implementation passes around shared_ptr instances by value in multiple places where they could be moved: 1) in the function void thread::_M_start_thread(__shared_base_type __b) the line _M_start_thread(__b, nullptr); could be changed to _M_start_thread(std::move(__b), nullptr); or alternatively take __shared_base_type by "universal reference" and forward it into the other _M_start_thread overload. 2) in the function void thread::_M_start_thread(__shared_base_type __b, void (*)()) the lines __b->_M_this_ptr = __b; int __e = __gthread_create(&_M_id._M_thread, &execute_native_thread_routine, __b.get()); could be changed to auto __p = __b.get(); __b->_M_this_ptr = std::move(__b); int __e = __gthread_create(&_M_id._M_thread, &execute_native_thread_routine, __p); 3) Finally, the use of shared_ptr seems wholly unnecessarily. This can likely be implemented more cleanly with unique_ptr, and more efficiently, since it avoids the extra heap space for the reference count and all of the atomic ops.