codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<time.h> #include<sys/time.h> void* Test(void* n) { long i=0, j=0; j = *(int*)n; for(i=0; i < 1000000000; i++) { //do nothing } printf("i = %d\n", j); pthread_exit(0); } int main(int argc, char* argv[]) { int i=0, ret=0, tmp=0, thread_num=10; clock_t t1, t2; struct timeval tv1, tv2; struct timezone tz; pthread_t thread[thread_num]; gettimeofday(&tv1, NULL); t1 = clock(); for(i=0; i<thread_num; i++){ ret = pthread_create(&thread[i], NULL, Test, (void*)&i); if(ret != 0){ fprintf(stderr, "thread create error!\n"); } } for(i=0; i<thread_num; i++){ ret = pthread_join(thread[i], NULL); if(ret != 0){ fprintf(stderr, "thread join error!\n"); } } gettimeofday(&tv2, NULL); t2 = clock(); printf("clock time = %f sec\n", (double)(t2 - t1)/CLOCKS_PER_SEC); printf("gettimeofday time = %f sec\n", (double)(tv2.tv_usec - tv1.tv_usec)/1000000); //printf("gettimeofday time = %f sec\n", (double)(tv2.tv_sec - tv1.tv_sec)); return 0; }
Private
[
?
]
Run code
Submit