I am trying to write code for finding if pairwise sums are even or not(among all possible pairs from 0 to 100000). I have written code using pthreads where the work allocation is done statically. Here is the code
#include<iostream>
#include<chrono>
#include<iomanip>
#include<pthread.h>
using namespace std;
#define MAX_THREAD 4
vector<long long> cnt(MAX_THREAD,0);
long long n = 100000;
int work_per_thread;
void *count_array(void* arg)
{
int t = *((int*)arg);
long long sum = 0;
int counter = 0;
for(int i = t*work_per_thread + 1; i <= (t+1)*work_per_thread; i++)
for(int j = i-1; j >= 0; j--)
{
sum = i + j;
if(sum%2 == 0)
counter++;
}
cnt[t] = counter;
cout<<"thread"<<t<<" finished work"<<endl;
return NULL;
}
int main()
{
pthread_t threads[MAX_THREAD];
vector<int> arr;
for(int i = 0; i < MAX_THREAD; i++)
arr.push_back(i);
long long total_count = 0;
work_per_thread = n/MAX_THREAD;
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < MAX_THREAD; i++)
pthread_create(&threads[i], NULL, count_array, &arr[i]);
for (int i = 0; i < MAX_THREAD; i++)
pthread_join(threads[i], NULL);
for (int i = 0; i < MAX_THREAD; i++)
total_count += cnt[i];
cout << "count is " << total_count << endl;
auto end = chrono::high_resolution_clock::now();
double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count();
time_taken *= 1e-9;
cout << "Time taken by program is : " << fixed << time_taken << setprecision(9)<<" secs"<<endl;
return 0;
}
Now I want to do the work allocation part dynamically. To be specific, let's say I have 5 threads. Initially I give the threads a certain range to work with, let's say thread1 works on all pairs from 0-1249, thread2 from 1250-2549 and so on. Now as soon as a thread completes its work I want to give it a new range to work on. This way no threads will be idle for most of the time, like was in the case of static allocation.