Created
May 18, 2014 06:28
-
-
Save termoshtt/96eaddc21775c0722c4a to your computer and use it in GitHub Desktop.
C++で簡単非同期処理(std::thread,std::async) ref: http://qiita.com/termoshtt/items/d3cb7fe226cdd498d2ef
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
auto th1 = std::thread([]{ do_long_work(); }); | |
do_another_things(); | |
th1.join(); |
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
double a; | |
auto th1 = std::thread([&a]{ a = long_calc(); }); | |
do_another_things(); | |
th1.join(); | |
std::cout << a << std::endl; // 1.0 |
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 <vector> | |
#include <future> | |
#include <iostream> | |
int main(int argc, char const *argv[]) { | |
std::vector<int> v(10); | |
std::vector<std::thread> threads; | |
for (int i = 0; i < 10; ++i) { | |
threads.push_back(std::thread([i, &v] { v[i] = i * i; })); | |
} | |
for (std::thread &th : threads) { | |
th.join(); | |
} | |
for (int i : v) { | |
std::cout << i << std::endl; | |
} | |
return 0; | |
} |
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
$ g++ -std=c++11 source.cpp -pthread |
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
$ clang++ -std=c++11 source.cpp -pthread |
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
terminate called after throwing an instance of 'std::system_error' | |
what(): Enable multithreading to use std::thread: Operation not permitted |
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
auto result = std::async(std::launch::async, [] { return long_calc(); }); | |
do_another_things(); | |
std::cout << result.get() << std::endl; |
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 <future> | |
#include <iostream> | |
#include <vector> | |
int main(int argc, char const *argv[]) { | |
std::vector<std::future<int> > v; | |
for (int i = 0; i < 10; ++i) { | |
v.push_back(std::async([i] { return i * i; })); | |
} | |
for (auto &val : v) { | |
std::cout << val.get() << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment