Terminate Boost::Thread

This is a discussion on Terminate Boost::Thread within the C++ Programming forums, part of the General Programming Boards category; I've been searching the documentation for Boost::Thread and I can't seem to figure out how to terminate a thread before ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Rochester, NY
    Posts
    27

    Terminate Boost::Thread

    I've been searching the documentation for Boost::Thread and I can't seem to figure out how to terminate a thread before it has completed I know when I was using the Windows Threading functions I was able to terminate a thread in the middle of execution. Is there anyway to do this with a Boost::Thread?

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2008
    Location
    Rochester, NY
    Posts
    27
    Never mind... did some more reading. Can't just call ThreadQuit() or something along those lines I have to essentially add a bunch of if statements checking if the thread should still be running. If so then do what needs to in order to close the thread.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Yep. Forcefully "killing" your thread is generally considered bad.

    If your thread performs multiple lengthy operations, as apposed to short loop, you just have have to scatter "if (I_should_exit)" about appropriately - where the signal to exit is accomplished using something like boost::condition.

    gg

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Since Boost 1.35, the thread class also has a member called interrupt(), which kills the thread, but only at some special interruption points. (Any of the sleeping functions of the thread library, basically.)

    In particular, the if(I_should_exit) could be simply written as boost::this_thread::interruption_point(), as long as your catch the boost::thread_interrupted that will be thrown.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. terminate() function
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2009, 09:37 AM
  2. Terminate() inside constructor
    By Mario F. in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2006, 03:59 AM
  3. how to terminate a target thread
    By swaugh in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2005, 10:13 AM
  4. how to terminate for loop
    By Chobo_C++ in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2004, 05:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21