What are pthread undo points used for?

There is a discussion of pthread undo points ( http://man7.org/linux/man-pages/man3/pthread_cancel.3.html ) on several issues. In some cases, respondents say cancel points should not be used unless the programmer knows what they are doing very well.

My question is, what are pthread undo points used for?

[updated from comment]

  • Does the cancel point allow cancellation of these specific API calls?
  • Why are those and not others?
  • Anyone want to use them for anything else?
  • Are they a hack to solve the kernel problem, or are they inherent in something related to POSIX?
  • Do you want to use user code cancel points or just in the API?
+7
c linux pthreads posix
source share
2 answers

Does the cancel point allow cancellation of these specific API calls?

Sticking to PTHREAD_CANCEL_DEFERRED, this can be a center of confusion. You almost think about it back. The cancellation point does not allow canceling system calls (the API in your item list), and the calls check to see if the cancellation request is waiting. Conceptually, you can think of it as (manually) by setting a switch. Automatic cancel points will check if this is valid, and a deliberate call to pthread_testcancel can be inserted where it is convenient or otherwise - for example, in the middle of a long calculation cycle, where otherwise system calls cannot be canceled. Cancel points automatically force calls for any pthread cleanup handlers that have been configured and that can be bound in a manner similar to atexit calls.

Many people choose (if feasible for their situation) to set their own switch and code as a means of disabling the flow at this point in the flow when checking the switch. PTHREAD_CANCEL_DEFERRED is mainly on steroids. It provides (potentially) several points where this switch is conceptually verified and thereby forces the developer to consider the consequences of canceling at each of these points.

Why are those and not others?

You will need to discuss this on a per-call basis, but primarily because of unpredictable side effects.

Do you want to use user code cancel points or just in the API?

Yes, you want to use them in user level code. You, as a programmer, are in a better position to know what resources you are using, and how to clear them if the stream is canceled, and the logical consequences of canceling at any given point.

PTHREAD_CANCEL_ASYNCHRONOUS is another ball of wax and a nightmare. Conceptually, you can think of it as a thread that exploded with the kill signal - it can be interrupted anywhere, but this allows launching cleanup handlers. The problem is that it is really hard to do, for example, what if it is canceled in the middle of malloc ? This makes it practically worthless outside of some really, really, carefully considered situations.

+3
source share

... what are pthread undo points used for?

From man 7 pthreads :

Cancellation points

POSIX.1 indicates that certain functions should and some other functions may be undo points.

More details:

If a thread is canceled, its cancellation type is canceled, and the cancellation request is pending for the thread, then the thread is canceled when it calls a function that is the cancellation point.

Referring to the questions:

  • The API functions defined by POSIX are here: http://man7.org/linux/man-pages/man7/pthreads.7.html (undo points) are either undo points or maybe .
  • To enter a (custom) specific cancellation item, pthread_testcancel() , where you think it may be necessary.
  • Not.
  • They are defined by POSIX to provide a standardized way to handle the cancellation method. This is especially important for locking functions.
  • see 2.

As a final note: Avoid canceling threads as much as possible, as this can lead to unexpected and irreproducible program behavior. Try to always develop a program so that it can gracefully close it, ending all threads without explicitly calling pthread_cancel() .

+3
source share

All Articles