• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: pthread_cancel and C++ destructors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pthread_cancel and C++ destructors


  • Subject: Re: pthread_cancel and C++ destructors
  • From: Ethan Tira-Thompson <email@hidden>
  • Date: Thu, 29 Nov 2007 23:45:24 -0500

First, let me say I'm excited to see that 10.5 has sleep(), read(), etc. as cancellation points.  
Argh, nevermind.  Further testing shows that read() is NOT A CANCEL POINT even in Leopard.  So the pthread_testcancel man page is definitely still giving incorrect information.  (just replace the sleep() in the launch function of the previous sample code with read(STDIN_FILENO,buf,n) to test...)  As far as i can tell, sleep (and friends usleep etc) and pthread_testcancel itself are the only thread cancel points.  I tried -D_APPLE_C_SOURCE to no avail.

Thus, if I want to break out of a read() (or apparently any other long-term blocking system call?), I still need to pthread_cancel, then pthread_kill to send a signal, and then have the signal handler do a pthread_testcancel.  However, 10.5 doesn't seem to like sending a signal to a thread that's been marked for cancellation (even though the thread isn't cancelled yet, still sitting in read()) – pthread_kill still returns a "no such process" error anyway).  The way I found to apply this workaround in both 10.4 and 10.5 is to send the signal first, and then do both the pthread_cancel and pthread_testcancel in the signal handler.  (Linux doesn't seem to like this approach very much however, but is a moot point since not needed there anyway.)

Any suggestions where best to complain about the cancel point issue?  I have no illusion this hack I'm using is horribly unsafe — essentially I'm just recreating PTHREAD_CANCEL_ASYNCHRONOUS, which means my thread could just stop executing at any time.

So anyway, in regards to my original question (cleanup of objects on the stack), I also just tried having a signal handler throw an exception, in hopes it would then unwind the stack back down to the original thread launch function (which would give me precisely what I'm looking for)  But apparently when the exception handling tries to unwind past the signal dispatch it just hands off to abort()/terminate() instead of unrolling through the rest of the stack.  wah, so close! :(

So, to summarize my issues with threads on OS X:

1. pthread_testcancel() (and sleep() variants in Leopard) are cancel points, read() and perhaps other useful points are not.  pthread_testcancel man page is incorrect in all versions of OS X. (does anyone have an accurate cancel point list?!?!)

2. read() cannot be made to return by interruption with a signal, although the man pages suggests a return code for that condition. (I was sending a SIGALRM with my own handler installed — is there a particular signal I need to send, perhaps without installing my own handler?)

3. pthread_mutex_unlock *does* sometimes work in a signal handler (where the lock was acquired in the thread before the signal.)  It works in my simplified test case, I haven't nailed down why it complains "operation not permitted" in my real code.

4. Exceptions thrown during signal handling can't fall through and be handled by the thread (perhaps this is required behavior by standards, but it sounds like a nice feature — I don't see how letting the exception unwind past the signal handler call could be any worse than just aborting on the spot.)

5. I'm basically recreating the asynchronous cancel type.... because it's apparently not supported at all on OS X (even ignores explicit pthread_testcancel calls, much less exit from busy loop!)

6. Still haven't found a way to release resources referenced on the stack — C++ destructors are not called, exception handling is not triggered.  (this causes memory leaks, invalid mutexes left locked, etc.  The C++ exception handling is a much better mechanism than the pthread_cleanup_* functions, and currently they don't play well together)

These are all pretty glaring problems IMHO :(

FWIW, on linux, the results are:
1. works with read() at least, probably others like it says it will
2. same results (doesn't actually return EINTR)
3. same results (works for the simple test case)
4. Works just like I suggest it should — exception rolls through signal handler and elegantly triggers thread cancellation.
5. asynchronous partially supported, seems to be well handled if thread was at a cancelation point, but aborts with "terminate called without an active exception" if in the middle of a busy loop.
6. Works beautifully... note that with this and #1 in place, makes the hacks in 2-5 relatively moot even if they didn't work.

I'll attach the 6 test cases because misery loves company :(
Or get it here: http://ethan.tira-thompson.com/stuff/pthread-tests.zip

As always, input appreciated!  At this point I'm just mulling over different portability and functionality issues... perhaps this thread will be useful to others. (as in, http://despair.com/mis24x30prin.html )

Thanks,
  -ethan

Attachment: pthread-tests.zip
Description: Zip archive

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: pthread_cancel and C++ destructors
      • From: "Lassi Tuura" <email@hidden>
    • Re: pthread_cancel and C++ destructors
      • From: Terry Lambert <email@hidden>
References: 
 >pthread_cancel and C++ destructors (From: Ethan Tira-Thompson <email@hidden>)

  • Prev by Date: Re: pthread_mutex_unlock not permitted in signal handler?
  • Next by Date: Re: pthread_cancel and C++ destructors
  • Previous by thread: pthread_cancel and C++ destructors
  • Next by thread: Re: pthread_cancel and C++ destructors
  • Index(es):
    • Date
    • Thread