Created
April 1, 2014 19:32
-
-
Save foobit/9921345 to your computer and use it in GitHub Desktop.
from: Herb Sutter's - Going Native 2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::shared_ptr<widget> get_widget(int id) | |
{ | |
static std::map<int, std::weak_ptr<widget>> cache; | |
static std::mutex m; | |
std::lock_guard<std::mutex> hold(m); | |
auto& wp = cache[id]; | |
auto sp = wp.lock(); | |
if (!sp) | |
{ | |
wp = sp = load_widget(id); | |
} | |
return sp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment