Created
February 13, 2014 07:45
-
-
Save lethee/8971334 to your computer and use it in GitHub Desktop.
future
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 <future> | |
#include <list> | |
// http://isocpp.org/blog/2013/07/what-is-stdpromise-stackoverflow | |
// g++ -std=c++0x -pthread test.cpp | |
// tested in gcc 4.6.3 | |
class OneTask { | |
std::promise<std::list<int> > resList; | |
public: | |
void Execute(int x) { | |
std::list<int> res; | |
for(int i=0; i<x; ++i) { | |
res.push_back(i); | |
} | |
resList.set_value(move(res)); | |
std::cout << "task run" << std::endl; | |
} | |
std::future<std::list<int> > Result() { | |
return resList.get_future(); | |
} | |
}; | |
int main(int argc, char **argv) { | |
OneTask ot; | |
std::future<std::list<int> > fut = ot.Result(); | |
std::thread th(&OneTask::Execute,&ot,2); | |
std::cout << "task queued" << std::endl; | |
std::list<int> res = fut.get(); | |
int r = res.size(); | |
std::cout << r << std::endl; | |
th.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment