Skip to content

Instantly share code, notes, and snippets.

@lethee
Created February 13, 2014 07:45
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 lethee/8971334 to your computer and use it in GitHub Desktop.
Save lethee/8971334 to your computer and use it in GitHub Desktop.
future
#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