Skip to content

Instantly share code, notes, and snippets.

@pancanin
Created October 4, 2022 11:34
Show Gist options
  • Save pancanin/96713bc948beb89c3316c4fa79fa0025 to your computer and use it in GitHub Desktop.
Save pancanin/96713bc948beb89c3316c4fa79fa0025 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <string>
#include <fstream>
#include <stack>
#include <unordered_set>
#include <queue>
#include <stack>
#include <limits>
#include <thread>
#include <set>
#include <array>
#include <future>
using namespace std;
int sum(vector<int>::iterator begin, vector<int>::iterator end, int count) {
int sum = 0;
while (begin != end) {
sum += *begin;
begin++;
}
return sum;
}
int main() {
vector<int> numbers = { 1,2,3,4,5,6 };
std::promise<int> sumPromise;
std::future<int> sumFuture = sumPromise.get_future();
auto job = std::thread([&] {
int val = sum(numbers.begin(), numbers.end(), numbers.size());
sumPromise.set_value(val);
});
int sumata = sumFuture.get();
job.join();
cout << sumata << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment