cplusplus:goingnative2013
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| cplusplus:goingnative2013 [2013/09/09 16:19] – created dblume | cplusplus:goingnative2013 [2023/04/12 20:44] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 24: | Line 24: | ||
|         cache[id] = sp = load_widget(id); |         cache[id] = sp = load_widget(id); | ||
| return sp; | return sp; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | In C++11, the following code is thread safe! It's the Meyers Singleton. | ||
| + | |||
| + | * Thread safe fn statics | ||
| + |   * Does destruction. No need for Alexandrescu' | ||
| + | |||
| + | <file cpp thread_safe_meyers_singleton.cpp> | ||
| + | widget& instance() { | ||
| + | static widget w; | ||
| + | return w; | ||
| } | } | ||
| </ | </ | ||
| Line 55: | Line 67: | ||
| CINDER_APP_BASIC( MyApp, RendererDx ) | CINDER_APP_BASIC( MyApp, RendererDx ) | ||
| </ | </ | ||
| + | |||
| + | ===== Advanced Optimization ===== | ||
| + | |||
| + | Use lambda in place of std::bind to save memory. A std::bind() on a pointer-to-member-function will allocate 24 bytes on the heap for the bind structure. If instead you use a lambda, then this fits into 4 bytes and avoids any allocation. | ||
| + | |||
| + | Replace: | ||
| + | |||
| + |     g(this, std:: | ||
| + | |||
| + | With: | ||
| + | |||
| + |     g(this, [this](){this-> | ||
| + | |||
| + | Note that subtle changes to object lifetimes is possible. This code: | ||
| + | |||
| + |     std:: | ||
| + | |||
| + | is not the same as this code: | ||
| + | |||
| + |     [n]{ f(std:: | ||
| + | |||
| + | The above code will bind the shared_ptr< | ||
| + | But it is the same as this: | ||
| + | |||
| + |     [wp = std:: | ||
| + | |||
| + | In this case the lambda' | ||
cplusplus/goingnative2013.1378768781.txt.gz · Last modified: 2023/04/12 20:44 (external edit)