Program utilizes threading to count to 1000000000
. There are no mutex locks and such since every thread accesses the same global variable and simply increases it.
// This is just a function pointer used later. Just for increasing the
// readability on following code.
typedef void (*fptr)(int);
void* count_to(void*);
double stopwatch(fptr, int);
// Number to sum to
const long long MAX_SUM = 1000000000;
// Global sum that threads accesses
long long sum = 0;
// Main function creates threads from 1 to 11 and count the time
// in between executions using `stopwatch` function.
int
main() {
for (int i = 1; i < 12; ++i) {
printf("\n======================================\n");
printf("Time it took when thread count is %d : %f",
i, stopwatch(create_and_execute, i));
printf("\n======================================\n");
}
return EXIT_SUCCESS;
}
// Takes a function pointer and thread count
double
stopwatch(fptr func, int count) {
clock_t begin = clock();
func(count);
clock_t end = clock();
return (double)(end - begin) / CLOCKS_PER_SEC;
}
void
create_and_execute(count) {
pthread_t* threads = (pthread_t*) malloc(sizeof(pthread_t) * count);
if (threads == NULL) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < count; ++i) {
if (pthread_create(&threads[i], NULL, count_to, NULL) != 0){
fprintf(stderr, "Can't create thread %d\n", i);
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < count; i++) {
pthread_join(threads[i], NULL);
}
printf("Threads finished their job. Sum is: %lld\n", sum );
sum = 0;
free(threads);
}
// counts to MAX_SUM
void*
count_to(void* i) {
while(sum < MAX_SUM) {
sum += 1;
}
pthread_exit(0);
}
Outputs
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 1 : 2.044165
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 2 : 5.137091
======================================
======================================
Threads finished their job. Sum is: 1000000001
Time it took when thread count is 3 : 7.673170
======================================
======================================
Threads finished their job. Sum is: 1000000001
Time it took when thread count is 4 : 20.567974
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 5 : 25.316267
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 6 : 14.618212
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 7 : 34.998254
======================================
======================================
Threads finished their job. Sum is: 1000000002
Time it took when thread count is 8 : 40.366402
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 9 : 38.578370
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 10 : 30.201249
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 11 : 42.017907
======================================
The problem is that, as you can see, increasing thread count just increases the execution time. With some exceptions. I was expecting it to be decrease the execution time greatly with increased thread count since threads work in parallel. I do understand that increasing the thread count after some thread count would just stay around the same as per law, as one can observe here too. But why is it that it increases instead of decreasing? Am I doing something wrong? Did I completely miss the point of threads? Any advice would be helpful.
To further illustrate my problem, my expected output would be, for example:
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 1 : 2.044165
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 2 : 1.137091
======================================
======================================
Threads finished their job. Sum is: 1000000001
Time it took when thread count is 3 : 1.073170
======================================
======================================
Threads finished their job. Sum is: 1000000001
Time it took when thread count is 4 : 1.007974
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 5 : 0.316267
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 6 : 0.618212
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 7 : 0.998254
======================================
======================================
Threads finished their job. Sum is: 1000000002
Time it took when thread count is 8 : 0.366402
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 9 : 0.578370
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 10 : 0.201249
======================================
======================================
Threads finished their job. Sum is: 1000000000
Time it took when thread count is 11 : 0.017907
======================================
If you think something is unclear, let me know!
PS: I originally asked this in codereview but someone suggested it fits better here. So, here I am.