Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created January 24, 2012 05:41
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 mastbaum/1668212 to your computer and use it in GitHub Desktop.
Save mastbaum/1668212 to your computer and use it in GitHub Desktop.
member function pointers in c++11
#include <iostream>
#include <thread>
/* g++ -std=c++0x -o thread-cpp11 thread-cpp11.cxx -lpthread */
class Bar {
public:
Bar() : i(0) {}
int i;
};
class Foo {
public:
Foo();
int threadMe(int arg);
private:
std::thread* fThread;
Bar bar;
};
Foo::Foo() {
bar.i = 42;
int arg = 12;
int ret;
// better to get return value with std::future
fThread = new std::thread{[this, arg, &ret](){ret = this->threadMe(arg);}};
fThread->join();
std::cout << ret << std::endl;
delete fThread;
}
int Foo::threadMe(int arg) {
std::cout << "Foo::threadMe: arg = " << arg << std::endl;
std::cout << "Foo::threadMe: bar.i = " << bar.i << std::endl;
return arg+1;
}
int main(int argc, char* argv[]) {
Foo* f = new Foo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment