too many threads



I'm making a small two button application to understand threads. Both are
radiobuttons. One button starts a thread (in the handler) and the second
button stops it. When I first start the application ps shows one process.
When I push "start", ps shows THREE threads. Finally, when I push "stop",
the spawned thread does stop, but ps shows 2 threads. Can someone shed
some light on this? Where does the extra thread come from?

Please see the sample code below. I only included the
relevant functions.

Thanks!


int stopped=0;

void * work(void * arg)
{
    int i=0;

    pthread_detach (pthread_self ());
    while(!stopped){
	printf("work: %d\n", i++);
    }

    pthread_exit(NULL);
}


void
on_radiobutton1_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
    pthread_t wt;

    stopped=0;
    if(gtk_toggle_button_get_active(togglebutton)){

	printf("START\n");
	if (pthread_create(&wt, NULL, work, NULL) != 0){
	    printf("Can't create work thread\n");
	    exit(-1);
	}

	printf("work done\n");
    }
}


void
on_radiobutton2_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
    if(gtk_toggle_button_get_active(togglebutton)){
        stopped=1;
	printf("STOP\n");
    }
}




int
main (int argc, char *argv[])
{
  GtkWidget *window1;

  g_thread_init(NULL);
  gnome_init ("thr", VERSION, argc, argv);

  window1 = create_window1 ();
  gtk_widget_show (window1);

  gdk_threads_enter();
  gtk_main ();
  gdk_threads_leave();

  return 0;
}






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]