#include #include #include #define WORK_SIZE 50000 float dest[WORK_SIZE]; int num_threads; // naive exponentiation is useful because it requires a lot of compute int power(int a, int b) { int i; int r = a; for (i = 1; i < b; i++) r = r * a; return r; } void* calc_powers(void* arg) { int start = *((int*)arg); int i = start * (WORK_SIZE / num_threads); for (int offset = 0; offset < WORK_SIZE / num_threads; offset++) { dest[i + offset] = power(i + offset, i + offset); } } int main(int argc, char** argv) { num_threads = 4; pthread_t threads[num_threads]; int starts[num_threads]; for (int i = 0; i < num_threads; i++) { starts[i] = i; pthread_create(&threads[i], NULL, &calc_powers, &starts[i]); } for (int i = 0; i < num_threads; i++) pthread_join(threads[i], NULL); printf("All done!\n"); }