Skip to content

Instantly share code, notes, and snippets.

@mlen
Created May 26, 2012 23:26
Show Gist options
  • Save mlen/2795607 to your computer and use it in GitHub Desktop.
Save mlen/2795607 to your computer and use it in GitHub Desktop.
// clang++ -std=c++11 -stdlib=libc++ main.cpp
#include <thread>
#include <vector>
#include <iostream>
using namespace std;
template <typename T> class sum {
vector<T> &v;
T &r;
T a;
public:
sum(vector<T> &data, T acc, T &result): v(data), a(acc), r(result) {}
void operator()();
};
template <typename T> void sum<T>::operator()() {
for (auto i: v) {
a += i;
}
r = a;
}
int main() {
int result;
vector<int> data(4, 100);
thread t(sum<int>(data, 0, result));
t.join();
cout << result << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment