Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. throw and catch

throw and catch

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Moldovan
    wrote on last edited by
    #1

    Does anyone know how to throw an exception across threads? Is this possible at all given the stack unwinding issues? Is there perhaps some other mechanism for interrupting the logic of one thread from another without Waits or SleepEx or other sync functions. Callbacks - from timers etc. - are in the calling thread so they are no good to me. I need to protect a block of code as follows, Start timer ------------- Code Block normal exit ------------- Timeout timeout exit The try/catch block is ideal but the timers run in their own threads so I don't catch nothing. Thanks to all!

    A 1 Reply Last reply
    0
    • J Joe Moldovan

      Does anyone know how to throw an exception across threads? Is this possible at all given the stack unwinding issues? Is there perhaps some other mechanism for interrupting the logic of one thread from another without Waits or SleepEx or other sync functions. Callbacks - from timers etc. - are in the calling thread so they are no good to me. I need to protect a block of code as follows, Start timer ------------- Code Block normal exit ------------- Timeout timeout exit The try/catch block is ideal but the timers run in their own threads so I don't catch nothing. Thanks to all!

      A Offline
      A Offline
      Andrew Peace
      wrote on last edited by
      #2

      AFAIK throwing an exception from a thread will cause the exception to be propogated down the stack of the thread only - if it were to be caught by another thread then the operating being performed by that thread would have to be interrupted etc and it would just be a disaster as well as being unimplentable (practially at least). A better solution is to return an error value from the thread. -- Andrew.

      J 1 Reply Last reply
      0
      • A Andrew Peace

        AFAIK throwing an exception from a thread will cause the exception to be propogated down the stack of the thread only - if it were to be caught by another thread then the operating being performed by that thread would have to be interrupted etc and it would just be a disaster as well as being unimplentable (practially at least). A better solution is to return an error value from the thread. -- Andrew.

        J Offline
        J Offline
        Joe Moldovan
        wrote on last edited by
        #3

        I think you are correct and a thread can't be interrupted. I don't see any great technical difficulty in doing it (that's really what the scheduler does after all) but the system is designed not to do it, so that's that. I looked at APCs but they only work when the thread is sleeping or waiting so that's no good to me either. I need to have functions (node/action functions for a state machine written by others) running by themselves and being timed out in the state engine which I wrote. What do you mean "return an error value from the thread"? Do you mean the value in the terminating "return int" or some other mechanism I don't know about. I know you can get the terminating value when a thread exits is this what you mean? Thanks for your thoughts anyway. Joe M.

        A 1 Reply Last reply
        0
        • J Joe Moldovan

          I think you are correct and a thread can't be interrupted. I don't see any great technical difficulty in doing it (that's really what the scheduler does after all) but the system is designed not to do it, so that's that. I looked at APCs but they only work when the thread is sleeping or waiting so that's no good to me either. I need to have functions (node/action functions for a state machine written by others) running by themselves and being timed out in the state engine which I wrote. What do you mean "return an error value from the thread"? Do you mean the value in the terminating "return int" or some other mechanism I don't know about. I know you can get the terminating value when a thread exits is this what you mean? Thanks for your thoughts anyway. Joe M.

          A Offline
          A Offline
          Andrew Peace
          wrote on last edited by
          #4

          I think you are correct and a thread can't be interrupted. I don't see any great technical difficulty in doing it... Yes, it is easy for threads to be suspended. However, to change the path of execution for a thread is unthinkable - I mean, if your program is carrying out a task and half way through writing to a file the control gets shifted to a code handler you've had it. Yes, I did mena the terminating return int statement. -- Andrew.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups