public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency
@ 2015-03-11 19:00 klemensbaum at gmail dot com
  2015-03-11 19:14 ` [Bug libstdc++/65393] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: klemensbaum at gmail dot com @ 2015-03-11 19:00 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/65393] std::thread shared_ptr inefficiency
  2015-03-11 19:00 [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency klemensbaum at gmail dot com
@ 2015-03-11 19:14 ` redi at gcc dot gnu.org
  2015-06-16 17:54 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2015-03-11 19:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65393

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2015-03-11
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
   Target Milestone|---                         |6.0
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Klemens from comment #0)
> 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.

(They're called forwarding references now)

That would require making the function a template, for no benefit.

It should be moved.


In general, these inefficiencies are pretty tiny compared to the cost of
launching a new thread that happens immediately after.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/65393] std::thread shared_ptr inefficiency
  2015-03-11 19:00 [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency klemensbaum at gmail dot com
  2015-03-11 19:14 ` [Bug libstdc++/65393] " redi at gcc dot gnu.org
@ 2015-06-16 17:54 ` redi at gcc dot gnu.org
  2015-06-22 15:54 ` redi at gcc dot gnu.org
  2015-09-29 12:56 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2015-06-16 17:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65393

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Author: redi
Date: Tue Jun 16 17:53:52 2015
New Revision: 224530

URL: https://gcc.gnu.org/viewcvs?rev=224530&root=gcc&view=rev
Log:
        PR libstdc++/65393
        * src/c++11/thread.cc (thread::_M_make_thread): Replace shared_ptr
        copies with moves.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/src/c++11/thread.cc


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/65393] std::thread shared_ptr inefficiency
  2015-03-11 19:00 [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency klemensbaum at gmail dot com
  2015-03-11 19:14 ` [Bug libstdc++/65393] " redi at gcc dot gnu.org
  2015-06-16 17:54 ` redi at gcc dot gnu.org
@ 2015-06-22 15:54 ` redi at gcc dot gnu.org
  2015-09-29 12:56 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2015-06-22 15:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65393

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Author: redi
Date: Mon Jun 22 15:53:27 2015
New Revision: 224742

URL: https://gcc.gnu.org/viewcvs?rev=224742&root=gcc&view=rev
Log:
Backport from mainline
2015-06-16  Jonathan Wakely  <jwakely@redhat.com>

        PR libstdc++/65393
        * src/c++11/thread.cc (thread::_M_make_thread): Replace shared_ptr
        copies with moves.

Modified:
    branches/gcc-5-branch/libstdc++-v3/ChangeLog
    branches/gcc-5-branch/libstdc++-v3/src/c++11/thread.cc


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/65393] std::thread shared_ptr inefficiency
  2015-03-11 19:00 [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency klemensbaum at gmail dot com
                   ` (2 preceding siblings ...)
  2015-06-22 15:54 ` redi at gcc dot gnu.org
@ 2015-09-29 12:56 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2015-09-29 12:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65393

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
shared_ptr is no longer used on trunk.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-09-29 12:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-11 19:00 [Bug libstdc++/65393] New: std::thread shared_ptr inefficiency klemensbaum at gmail dot com
2015-03-11 19:14 ` [Bug libstdc++/65393] " redi at gcc dot gnu.org
2015-06-16 17:54 ` redi at gcc dot gnu.org
2015-06-22 15:54 ` redi at gcc dot gnu.org
2015-09-29 12:56 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).