Created
January 24, 2012 05:41
-
-
Save mastbaum/1668212 to your computer and use it in GitHub Desktop.
member function pointers in c++11
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
#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