I'm trying to parallelize a simple task using pthreads but I'm not entirely sure how to do the implementation. My problem is that I have n vectors of length L, on each of which I need to compute something that takes O(L) time, and both n and L are large, so I think it's in my best interest to parallelize. Unfortunately I am on a shared server and I believe that many other people would be quite upset with me if I created n threads and took up all the CPUs, so I'd like to do something like define a fixed number of threads that I can use and then have something like:
1 2 3 4 5 6 7
for (i = 0; i < n; i++) {
if (threads are available) {
pthread_create(...)
} else {
wait for a thread to finish then start
}
}