Killing Blocked Thread

This is a discussion on Killing Blocked Thread within the C Programming forums, part of the General Programming Boards category; Hi Guys, I am after some advice. I currently have a thread which is blocked which i require to kill. ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Killing Blocked Thread

    Hi Guys,

    I am after some advice. I currently have a thread which is blocked which i require to kill. I have spent ages looking around and although i have read it shouldn't be done but its the only way around what i've gotta do.

    My Question is how do i kill this blocked thread?

    Any help or guidance would be great

    Cheers

    Dan

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    What sort of system/threading library are you using? Nobody will be able to help you very much without that sort of info...

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On which Operating System? Windows, Mac, *nix ?

    On Windows you should be able to use the TerminateThread() function...

    TerminateThread Function (Windows)

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Hi, sorry we are using pthreads on Linux POSIX.


    Thanks Dan.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    On which Operating System? Windows, Mac, *nix ?

    On Windows you should be able to use the TerminateThread() function...

    TerminateThread Function (Windows)
    This is the kinda thing i am after for pthreads on Linux POSIX

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Did you look at pthread_cancel?

    EDIT: or pthread_kill. There's a whole list of pthread functions (and a decent general reference) here: https://computing.llnl.gov/tutorials/pthreads/.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> I currently have a thread which is blocked which i require to kill.
    "Killing" is rarely the answer. Where is it "blocked"?

    gg

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Kill works from within itselft but not from another thread, cancel has no effect :-(.

    Essentially we want to kill from another thread. i dunno if killing is right way, but basically we need to stop the tread that is running.

    thanks again for any ideas

    Dan

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Did you actually look into pthread_kill? Not only does the prototype show a thread ID parameter, suggesting you can kill a thread other than the current one, the description states it explicitly:
    Quote Originally Posted by man pthread_kill
    DESCRIPTION
    The pthread_kill() function sends the signal sig to thread, another thread in the same process as the caller.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You should never need to call pthread_kill() in a properly written application. If your thread has conditions where it blocks, then you need to wake it up and have it return cleanly from the thread procedure function.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Agreed pthread_kill is not ideal, but I wouldn't say that one "should never need to call" it. It simply sends an asynchronous signal to another thread. Sending something like SIGKILL would actually terminate the whole process, but sending something like SIGUSR1 would be, IMO, a completely acceptable solution to asynchronous thread communication. The signal handler for that signal could call cleanup code followed by pthread_exit. Besides, pthread_cancel is at the mercy of the thread being cancelled, which is an attribute set by the thread itself. This is probably not the case, but the OP may not have control over those parts of the code that caused the rogue thread to block, and since it's blocking, that thread can't make itself cancelable if it currently isn't, and can't necessarily unblock itself. I still agree that well written code should avoid potential blocking issues, but that doesn't necessarily solve the OP's problem.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by anduril462 View Post
    ...but that doesn't necessarily solve the OP's problem.
    I've never seen a threading problem where the solution is to kill the thread. In fact, many modern implementations of pthread_kill() will outright crash the application.
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Indeed, I've never used pthread_kill myself (though I'm not a frequent user of pthreads). The crash is only an issue if you're signalling an already closed thread though. As the the discussion mentions, "In NPTL pthread_t is a pointer to the thread's control structure, that is freed after either the thread has been pthread_join'ed, or has exited when in detached state. Accessing it afterwards is similar to passing a fclosed FILE * descriptor to fwrite, etc.". Nobody would advocate "never calling fwrite" because of this. The caller simply must be mindful of the state of that thread ID before calling pthread_kill. It also allows asynchronous behavior, which can't be accommodated by any other method that I'm aware of.

    @danderton: Is this an academic exercise where the point is actually to kill another thread, or is this a practical problem, where you have a blocked thread that you don't know how to handle? How exactly is it "blocking"? Is it stuck on a blocking I/O call? Is it blocked on a condition variable (e.g. using pthread_cond_wait)?
    • If the point is strictly to kill a thread other than the current one, then pthread_cancel or pthread_kill may be what you have to do.
    • If it is blocked using pthread_cond_wait, look into pthread_cond_signal
    • If it is blocked by something like an I/O operation, you may want to look into using non-blocking I/O with a select to eliminate the blocking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. blocked sites
    By munna_dude in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-23-2007, 04:53 AM
  2. Terminating blocked thread (MS VC++)
    By IndigoTangoOne in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 06:56 AM
  3. Killing a thread which is waiting
    By yoxler in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2003, 04:32 AM
  4. Blocked...Blocked
    By teresius in forum C Programming
    Replies: 4
    Last Post: 09-02-2001, 09:18 AM

Tags for this Thread


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