#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void update_a(vector<int> *va, vector<int> *vb) {
for (size_t i = 0; i < va->size(); ++i) {
// range safty checker
if (i < (vb->size() - 1))
va->at(i) += vb->at(i + 1) - vb->at(i);
cout << va->at(i) << endl;
}
cout << "T1 Done.\n\n";
}
void update_b(vector<int> *vb, vector<int> *va) {
for (size_t i = 0; i < vb->size(); ++i) {
// range safty checker
if (i < (va->size() - 1))
vb->at(i) += va->at(i + 1) - va->at(i);
cout << vb->at(i) << endl;
}
cout << "T2 Done.\n\n";
}
int main()
{
vector<int> a = {1,2,3,4,5};
vector<int> b = {1,2,3,4,5};
// Constructing two threads and run it. Does not block execution.
thread t1(update_a, &a, &b);
thread t2(update_b, &b, &a);
cout << "main, t1 and t2 functions now execute concurrently...\n";
// synchronize threads
t1.join(); // Makes the main thread wait until t1 finishes
t2.join(); // Makes the main thread wait until t2 finishes
}