Specialities of std::shared_ptr

Contents[Show]

After I draw the big picture of a std::shared_ptr in the last post, I want to present two special aspects of this smart pointer in this post. First, I show with std::shared_from_this how to create a std::shared_ptr from an object; second, I'm interested in the question to the answer: Should a function take a std::shared_ptr by copy or by reference? The numbers are quite interesting.

 

std::shared_ptr from this

Thanks to std::enable_shared_from_this, you can create an object that returns a std::shared_ptr from this. Therefore, the class of the objects has to be public derived from std::enable_shared_from_this. Now, you have the method shared_from_this available, which you can use to create std::shared_ptr from this.

The program shows the theory in practice.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// enableShared.cpp

#include <iostream>
#include <memory>

class ShareMe: public std::enable_shared_from_this<ShareMe>{
public:
  std::shared_ptr<ShareMe> getShared(){
    return shared_from_this();
  }
};

int main(){

  std::cout << std::endl;

  std::shared_ptr<ShareMe> shareMe(new ShareMe);
  std::shared_ptr<ShareMe> shareMe1= shareMe->getShared();
  {
    auto shareMe2(shareMe1);
    std::cout << "shareMe.use_count(): "  << shareMe.use_count() << std::endl;
  }
  std::cout << "shareMe.use_count(): "  << shareMe.use_count() << std::endl;
  
  shareMe1.reset();
  
  std::cout << "shareMe.use_count(): "  << shareMe.use_count() << std::endl;

  std::cout << std::endl;

}

 

The smart pointer shareMe (line 17) and it copies shareMe1 (line 18) and shareMe2 (line 20) reference the very same resource and increment and decrement the reference counter.

 enabledShared

The call shareMe->getShared() in line 18 creates a new smart pointer. getShared() internally uses (line 9) the function shared_from_this.

There is something very special about the class ShareMe.

Curiously recurring template pattern 

ShareMe is the derived class and type argument (line 6) of the base class std::enabled_shared_from_this. This pattern is called CRTP, an abbreviation for Curiously Recurring Template Pattern. Obviously, there is no recursion because the base class methods will be instantiated when they are called. CRTP is an often-used idiom in C++ to implement static polymorphism. Unlike dynamic polymorphism with virtual run-time methods, static polymorphism occurs at compile time.

But now, back to the std::shared_ptr.

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:

 

 

 

 

Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.

std::shared_ptr as a function argument

Therefore, we are dealing with a quite interesting question. Should a function take its std::shared_ptr by a copy of by reference? But first. Why should you care? Does it matter if a function takes its std::shared_ptr by copy or reference? Under the hood, all is a reference. My definite answer is yes, and no. Semantically, it makes no difference. From the performance perspective, it makes a difference.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// refVersusCopySharedPtr.cpp

#include <memory>
#include <iostream>

void byReference(std::shared_ptr<int>& refPtr){
  std::cout << "refPtr.use_count(): " << refPtr.use_count() << std::endl;
}

void byCopy(std::shared_ptr<int> cpyPtr){
  std::cout << "cpyPtr.use_count(): " << cpyPtr.use_count() << std::endl;
}


int main(){

    std::cout <<  std::endl;

    auto shrPtr= std::make_shared<int>(2011);

    std::cout << "shrPtr.use_count(): " << shrPtr.use_count() << std::endl;

    byReference(shrPtr);
    byCopy(shrPtr);
    
    std::cout << "shrPtr.use_count(): " << shrPtr.use_count() << std::endl;
    
    std::cout << std::endl;
    
}

 

The function byReference (line 6 - 8) and byCopy (line 10 - 12) takes their std::shared_ptr by reference and copy. The output of the program emphasizes the critical point.

 refVersusCopySharedPtr

The function byCopy takes its std::shared_ptr by copy. Therefore, the reference count increases in the function body to 2 and decreases to 1. The question is now. How expensive is the incrementing and decrementing of the reference counter? Because incrementing the reference counter is an atomic operation, I expect a measurable difference. To be precise. The incrementing of the reference counter is an atomic operation with relaxed semantics; the decrementing is an atomic operation with acquire-release semantics.

Let's have a look at the numbers.

Performance comparison

My Linux PC is more powerful than my Windows PC. Therefore, you must read the absolute numbers with a grain of salt. I use GCC 4.8 and Microsoft Visual Studio 15. Additionally, I translate the program with maximum and without optimization. At first, my small test program.

In the test program, I hand over the std::shared_ptr by reference and copying and using the std::shared_ptr to initialize another std::shared_ptr. This was the most straightforward scenario to cheat the optimizer. I invoke each function 100 million times.

The program

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// performanceRefCopyShared.cpp

#include <chrono>
#include <memory>
#include <iostream>

constexpr long long mill= 100000000;

void byReference(std::shared_ptr<int>& refPtr){
  volatile auto tmpPtr(refPtr);
}

void byCopy(std::shared_ptr<int> cpyPtr){
  volatile auto tmpPtr(cpyPtr);
}


int main(){

    std::cout <<  std::endl;
    
    auto shrPtr= std::make_shared<int>(2011);
   
    auto start = std::chrono::steady_clock::now();
  
    for (long long i= 0; i <= mill; ++i) byReference(shrPtr);    
    
    std::chrono::duration<double> dur= std::chrono::steady_clock::now() - start;
    std::cout << "by reference: " << dur.count() << " seconds" << std::endl;
    
    start = std::chrono::steady_clock::now();
    
    for (long long i= 0; i<= mill; ++i){
        byCopy(shrPtr);
    }
    
    dur= std::chrono::steady_clock::now() - start;
    std::cout << "by copy: " << dur.count() << " seconds" << std::endl;
    
    std::cout << std::endl;
    
}

 

First, the program is without optimization.

Without optimization

 performanceperformanceWindows

And now, the one with maximum optimization.

With maximum optimization

performanceOptimizationperformanceOptimizationWindows

My conclusion

comparisonEng

The raw numbers of the program performanceCopyShared.cpp speak a clear message.

  • The perReference function is about two times faster than its pendant perCopy, with maximum optimization on Linux about five times faster.
  • Maximum optimization gives Windows a performance boost of 3; on Linux by a factor of 30 - 80.
  • The Windows application is without optimization faster than the Linux application. That's interesting because my Windows PC is slower.

What's next?

The classical issue of smart pointers using reference count is to have cyclic references. Therefore, std::weak_ptr comes to our rescue. I will have in the next post a closer look at std::weak_ptr and show you how to break cyclic references.

 

 

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

 

My special thanks to Take Up Code TakeUpCode 450 60

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

 

 

Comments   

0 #1 mehndi clothes 2017-02-22 08:19
Very great post. I just stumbled upon your blog and wanted to
say that I have really loved browsing your weblog posts.
After all I'll be subscribing to your feed and I am hoping you write onhe more soon!
Quote

Stay Informed about my Mentoring

 

Mentoring

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 4032

Yesterday 4344

Week 40910

Month 21156

All 12099365

Currently are 158 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments