@@ -611,7 +611,7 @@ pthread_handler_t worker_thread(void *arg)
611611 pthread_cond_signal (& count_threshhold );
612612 pthread_mutex_unlock (& counter_mutex );
613613 mysql_thread_end ();
614-
614+ pthread_exit ( 0 );
615615 return 0 ;
616616}
617617
@@ -637,15 +637,30 @@ int main(int argc, char **argv)
637637
638638 if (opt_use_threads && !lock_tables )
639639 {
640- pthread_t mainthread ; /* Thread descriptor */
641- pthread_attr_t attr ; /* Thread attributes */
640+ char * * save_argv ;
641+ uint worker_thread_count = 0 , table_count = 0 , i = 0 ;
642+ pthread_t * worker_threads ; /* Thread descriptor */
643+ pthread_attr_t attr ; /* Thread attributes */
642644 pthread_attr_init (& attr );
643645 pthread_attr_setdetachstate (& attr ,
644- PTHREAD_CREATE_DETACHED );
646+ PTHREAD_CREATE_JOINABLE );
645647
646648 pthread_mutex_init (& counter_mutex , NULL );
647649 pthread_cond_init (& count_threshhold , NULL );
648650
651+ /* Count the number of tables. This number denotes the total number
652+ of threads spawn.
653+ */
654+ save_argv = argv ;
655+ for (table_count = 0 ; * argv != NULL ; argv ++ )
656+ table_count ++ ;
657+ argv = save_argv ;
658+
659+ if (!(worker_threads = (pthread_t * ) my_malloc (table_count *
660+ sizeof (* worker_threads ),
661+ MYF (0 ))))
662+ return -2 ;
663+
649664 for (counter = 0 ; * argv != NULL ; argv ++ ) /* Loop through tables */
650665 {
651666 pthread_mutex_lock (& counter_mutex );
@@ -660,15 +675,16 @@ int main(int argc, char **argv)
660675 counter ++ ;
661676 pthread_mutex_unlock (& counter_mutex );
662677 /* now create the thread */
663- if (pthread_create (& mainthread , & attr , worker_thread ,
664- (void * )* argv ) != 0 )
678+ if (pthread_create (& worker_threads [ worker_thread_count ] , & attr ,
679+ worker_thread , (void * )* argv ) != 0 )
665680 {
666681 pthread_mutex_lock (& counter_mutex );
667682 counter -- ;
668683 pthread_mutex_unlock (& counter_mutex );
669- fprintf (stderr ,"%s: Could not create thread\n" ,
670- my_progname ) ;
684+ fprintf (stderr ,"%s: Could not create thread\n" , my_progname );
685+ continue ;
671686 }
687+ worker_thread_count ++ ;
672688 }
673689
674690 /*
@@ -686,6 +702,14 @@ int main(int argc, char **argv)
686702 pthread_mutex_destroy (& counter_mutex );
687703 pthread_cond_destroy (& count_threshhold );
688704 pthread_attr_destroy (& attr );
705+
706+ for (i = 0 ; i < worker_thread_count ; i ++ )
707+ {
708+ if (pthread_join (worker_threads [i ], NULL ))
709+ fprintf (stderr ,"%s: Could not join worker thread.\n" , my_progname );
710+ }
711+
712+ my_free (worker_threads );
689713 }
690714 else
691715 {
0 commit comments