0

I have have a problem with the Thread Termination ([C] - Linux). The problem is that I can not completely finish a thread and deallocate resources. Because if I recall the program, I see that the old thread is still running

The server code is more less like this:

int quit=0;
void* sending();

main(){
.......
pthread_t thread_send;
pthread_create(&thread_send, NULL, sending, NULL);

//pthread_join(thread_send); here is useful? the thread don't completely finish-

return 0;
}


void *sending(){
   while(quit==0){

      if(...){
         quit=1;
      }

   }
//pthread_exit(NULL); here is useful ?
return 0;
}

How i can use pthred_exit() or pthred_kill(); or pthread_detach or pthread_join? I would like to terminate the thread properly and deallocating all the memory used by the thread.

thank you!

Skipper
  • 83
  • 1
  • 2
  • 8
  • Why is `quit` global? Maybe a data race if the main thread writes to it? – usr Mar 24 '14 at 20:57
  • Do you want `main` to kill the thread using the `quit` variable, or do you just want `main` to wait for the thread to exit, or do you want the thread to quit and cleanup while main continues to run? – user3386109 Mar 24 '14 at 21:28
  • both the first and the third option =) So, i want to terminate the thread, using the global quit variable (to exit the infinite while loop). Moreover when i terminate the thread i want to completely cleanup the memory, so that only the main remains. My problem in fact is that when i restart the code i see the thread still alive. – Skipper Mar 25 '14 at 08:55
  • What do you mean by "i see the thread still alive"? Is it still doing something? What do you think about the data race that I mentioned? – usr Mar 25 '14 at 15:45
  • "i see the thread still alive" = i start the program ./program -> i make quit so the if condition is true -> the thread function go to return 0. and the main finish. if i recall the program (./program) e see the sending function like quit=0. // On the contrary, when i make quit (condition if true), i want to completely cleanup the memory. isn't there a kill thread function o similar? – Skipper Mar 25 '14 at 18:50
  • After a process has exited, all memory and threads are permanently destroyed. You seem to assume that something has survived, which is not the case. Therefore, you must be misinterpreting some observation. Any idea what it could be? – usr Mar 25 '14 at 19:58
  • yes, maybe you're right! I think I found a possible problem ;) Thank you at all. But @urs then, the functions of the join, cancel, kill? I can not use them if I terminate a thread properly with a return 0, for example? – Skipper Mar 27 '14 at 16:23

0 Answers0