Skip to content

Instantly share code, notes, and snippets.

@kittinunf
Last active April 22, 2018 05:30
Show Gist options
  • Save kittinunf/e7b5e000228942f9ba3b67813aca7786 to your computer and use it in GitHub Desktop.
Save kittinunf/e7b5e000228942f9ba3b67813aca7786 to your computer and use it in GitHub Desktop.
#include <string>
#include <chrono>
#include <thread>
#include <iostream>
using namespace std::chrono;
auto calculation1(std::string d) {
std::this_thread::sleep_for(seconds(5));
return "cal1_" + d;
}
auto calculation2(std::string d) {
std::this_thread::sleep_for(seconds(5));
return "cal2_" + d;
}
int main() {
auto start = system_clock::now();
auto data1 = calculation1("Data");
auto data2 = calculation2("Data");
auto end = system_clock::now();
auto diff = duration_cast<seconds>(end - start).count();
std::cout << "Total Time = " << diff << " Seconds" << std::endl;
//Combine The Data
auto data = data1 + "::" + data2;
//Printing the combined Data
std::cout << "Data = " << data << std::endl;
return 0;
}
#include <string>
#include <chrono>
#include <thread>
#include <iostream>
#include <future>
using namespace std::chrono;
auto calculation1(std::string d) {
std::this_thread::sleep_for(seconds(5));
return "cal1_" + d;
}
auto calculation2(std::string d) {
std::this_thread::sleep_for(seconds(5));
return "cal2_" + d;
}
int main() {
auto start = system_clock::now();
//auto data1 = calculation1("Data");
auto futureData1 = std::async(std::launch::async, calculation1, "Data");
auto data2 = calculation2("Data");
// auto resultData2 = std::async(std::launch::async, calculation2, "Data");
auto end = system_clock::now();
auto diff = duration_cast<seconds>(end - start).count();
std::cout << "Total Time = " << diff << " Seconds" << std::endl;
//Combine The Data
//auto data = data1 + "::" + data2;
auto data = futureData1.get() + "::" + data2;
//Printing the combined Data
std::cout << "Data = " << data << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment