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.

Reply via email to