- 論壇徽章:
- 0
|
本帖最后由 mjus 于 2018-03-12 20:52 編輯
- #include<stdio.h>
- #include<pthread.h>
- #include<math.h>
- #include<unistd.h>
- #define NBR_THRS 8
- void* busywork(void* arg) {
- long t = (long) arg;
- double res = 0;
- for(long i =0; i< t; i++)
- res += sin(i) * cos(i);
- //printf("res=%f\n", res);
- return NULL;
- }
- int main() {
- pthread_t ID[NBR_THRS];
- long i;
- for(i=0; i<NBR_THRS; i++)
- pthread_create(&ID[i], NULL, busywork, (void*) 100000000);
- for(i=0; i<NBR_THRS; i++)
- pthread_join(ID[i], NULL);
- }
復(fù)制代碼
本程序若去除子程序中的打印語(yǔ)句,在我的電腦上運(yùn)行僅花0.002秒;若加上子程序中的打印語(yǔ)句,則花20.635秒之多!請(qǐng)大俠們解釋下為啥差別那么大?編譯都用 gcc -Wall -O3 demo.c -lpthread -lm
|
|