Thread: Terminating secondary thread from another thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    exit() is a very dangerous solution, because destructor's of automatic objects are not called, so any kind of object-bound resource is not properly freed.

    1. use cancellation points and call cancel/join from your main thread, or
    2. on threads with endless loop introduce a flag, so the loop is ended if flag is set from main thread. after setting flag call join

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pheres View Post
    exit() is a very dangerous solution, because destructor's of automatic objects are not called, so any kind of object-bound resource is not properly freed.
    Because cancelling a thread calls destructors?

    (It doesn't.)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by brewbuck View Post
    Because cancelling a thread calls destructors?
    (It doesn't.)
    But the thread knows when it possibly gets canceled (because it knows in cancellation points) but it doesn't know when it gets killed by exit()?

    But of course I would use the second solution, too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM