What are pthread cancelation points used for?

前端 未结 2 728
时光说笑
时光说笑 2021-01-12 08:33

There is discussion of pthread cancellation points (http://man7.org/linux/man-pages/man3/pthread_cancel.3.html) on several question. In some cases the respondents say that c

2条回答
  •  误落风尘
    2021-01-12 09:17

    ... what are pthread cancellation points used for?

    From man 7 pthreads:

    Cancellation points

    POSIX.1 specifies that certain functions must, and certain other functions may, be cancellation points.

    More:

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

    Referring the questions:

    1. The API functions defined by POSIX here: http://man7.org/linux/man-pages/man7/pthreads.7.html (Cancellation Points) either are cancellations points or may be some.
    2. To introduce a (user-) specific cancellation point call pthread_testcancel() where you feel it might be necessary.
    3. No.
    4. They are defined by POSIX to provide a standardise way to handle the way cancellation might happen. This especially makes sense for blocking functions.
    5. see 2.

    As a final note: Avoid cancellation of threads whereever possible, as it might lead to unexpected and unreproducable behaviour of a program. Try to always design a program to be able to gracefully shut it down by ending all threads without explicit calling pthread_cancel().

提交回复
热议问题