程序利用线程计数到1000000000
。没有互斥锁等,因为每个线程都访问相同的全局变量并且只是增加它。
// 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);
}
输出
======================================
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
======================================
问题在于,正如您所看到的,增加线程数会增加执行时间。除了一些例外。由于线程并行工作,我期望它会随着线程数的增加而大大减少执行时间。我确实理解,在一些线程计数之后增加线程数将保持与法律相同,正如人们在这里也可以观察到的那样。但为什么它增加而不是减少呢?难道我做错了什么?我完全错过了线程吗?任何建议都会有所帮助。
为了进一步说明我的问题,我的预期输出将是,例如:
======================================
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
======================================
如果你认为某些事情不清楚,请告诉我!
PS:我最初在codereview中问过这个问题,但是有人建议它在这里更合适。所以,我在这里。