Thread: Quick thread question.

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Quick thread question.

    Problem: A pthread_t thread is spawned and running, and user desires to stop it and
    save the partial processing of the thread to a file.

    Question: What is the proper way to kill a thread? SIGKILL, and SIGSTOP terminate the parent process too. Is it SIGHUP?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    The thread is created using the following statement.
    Code:
    my_ret = pthread_create( &my_thread, NULL, thread_is_prime, &my_param);
    I tried stopping it with:
    Code:
    pthread_kill(my_thread, SIGSTOP);
    But that causes main() to exit also.
    The thread is not joined to the main thread using thread_join().
    I read about the sigpending function in the manual pages, and it seems that i should try to use it to catch the signal sent by the main thread, and call thread_exit(), BUT the pages state that the sigpending function will make the thread wait for the signal flags of it's parameters to be set, which is not what i want i think.
    Any suggestions? I haven't tried concurrent programming before, but it seems like a very powerful way to code, and i would like to get started.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by xuftugulus View Post
    The thread is created using the following statement.
    Code:
    my_ret = pthread_create( &my_thread, NULL, thread_is_prime, &my_param);
    I tried stopping it with:
    Code:
    pthread_kill(my_thread, SIGSTOP);
    Not so sure if it actually is the right way to do it, but further reading suggested that pthread_cancel() might be what i needed.
    Code:
    printf("Invoking pthread_cancel()... \n");
    pthread_cancel(my_thread);
    Thread died, main() lived but still i feel that installing a signal handler for the thread so that proper cleanup will be performed is a much better solution. Need to dig up a little more though... all the POSIX signal stuff is a bit too technical for rush application...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    As a general rule, it's not a good idea to forcibly cancel or terminate threads, except as a last resort. It's better to set up some scheme to communicate with a thread function, and write the thread function so it terminates normally when told to.

    For example, main() sets some global variable to 42 when it wants the thread to exit. The thread function periodically checks that global, and exits when it finds a value of 42.

    Forcibly terminating threads can, depending on operating system, result in resource leakage .... not a particularly good thing to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Quick File Input Question
    By ProjectsProject in forum C Programming
    Replies: 2
    Last Post: 11-04-2004, 11:39 AM
  3. Quick question
    By Rapt in forum C Programming
    Replies: 5
    Last Post: 11-01-2003, 02:16 AM
  4. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  5. just a reaaaaaaly quick question plz help
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2002, 11:39 AM