Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Forked from njlr/shared_ptr_example.cpp
Last active May 26, 2017 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhedonia/25f734b867b2b0fb92ed9d8171963d54 to your computer and use it in GitHub Desktop.
Save nikhedonia/25f734b867b2b0fb92ed9d8171963d54 to your computer and use it in GitHub Desktop.
struct Texture {
  Texture(string const& path) { cout << "Texture loaded from " << path << endl; }
 ~Texture() { cout<< "Texture destroyed" << endl; }
 
  static shared_ptr<Texture> load(string const& path) {
  return make_shared<Texture>(path)
  }
};
// this functions will take a copy and increment the counter
// using const& would not increment the ref-counter
int doSomethingWithTexture(shared_ptr<Texture> const tex);
int doSomethingElseWithTexture(shared_ptr<Texture> const tex);
void foo() {
 shared_ptr<Texture> tex = Texture::load("textures/my_texture.png");
 
  thread(doSthWithTexture, tex).detach();
  thread(doSthMoreWithTexture, tex).detach();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment