I am asked to write an OpenMP program in C such that the main thread distributes the work to other threads, and while they are working on their tasks, the main should periodically check whether they are done, and if not, it should increment a shared variable.
This is the function for the threads' task:
void work_together(int *a, int n, int number, int thread_count) {
# pragma omp parallel for num_threads(thread_count) \
shared(a, n, number) private(i) schedule(static, n/thread_count)
for (long i=0; i<n; i++) {
// do a task, such as:
a[i] = a[i] * number;
}
}
And it gets called from main:
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
int arr[n];
initialize(arr, n);
// this will be the shared variable
int number = 2;
work_together(arr, n, number, thread_count);
//I want to write a function or an if to check whether threads are still working
/* if (threads_still_working()) {
number++;
sleep(100);
}
*/
printf("There are %d threads\n", omp_get_num_threads());
}
thread_count is initialized as 4, and I tried executing it for large n's (>10000), but the master thread will always wait for the other threads to finish executing the for loop, and will only continue the main when work_together() returns: the printf() will always print that there's only one thread running.
Now, what would be a way to check from the master thread whether the other threads are still running, and do some incrementing if they are?