1 /******************************************************************************* 2 * twordcount.c threaded word counter for two files 3 */ 4 #include <stdio.h> 5 #include <pthread.h> 6 #include <ctype.h> 7 8 int total_words; 9 pthread_mutex_t counter_lock=PTHREAD_MUTEX_INITIALIZER;//互斥锁 10 11 int main(int argc, char* argv[]) { 12 pthread_t t1, t2; 13 void * count_words(void*); 14 pthread_create(&t1, NULL, count_words, (void*)argv[1]); 15 pthread_create(&t2, NULL, count_words, (void*)argv[2]); 16 pthread_join(t1, NULL); 17 pthread_join(t2, NULL); 18 printf("%5d: total words ", total_words); 19 } 20 21 void* count_words(void* file) { 22 char* filename=(char* ) file; 23 FILE* fp; 24 int c, prevc='