Книга: Programming with POSIX® Threads

9.3.6 Cancellation

9.3.6 Cancellation

Cancellation provides a way to request that a thread terminate "gracefully" when you no longer need it to complete its normal execution. Each thread can control how and whether cancellation affects it, and can repair the shared state as it terminates due to cancellation.

pthread_cancel

int pthread_cancel (

pthread_t thread);

Requests that thread be canceled.

References: 5.3 Headers: <pthread.h>

Errors: [ESRCH] no thread found corresponding to thread.

Hint: Cancellation is asynchronous. Use pthread_join to wait for termi-

nation of thread if necessary.

pthread_cleanup_pop

void pthread_cleanup_pop (int execute);

Pop the most recently pushed cleanup handler. Invoke the cleanup handler if execute is nonzero.

References: 5.3 Headers: <pthread.h>

Hint: Specify execute as nonzero to avoid duplication of common cleanup

code.

pthread_cleanup_push

void pthread_cleanup_push (

void (*routine)(void *),

void *arg);

Push a new cleanup handler onto the thread's stack of cleanup handlers. Invoke the cleanup handler if execute is nonzero. Each cleanup handler pushed onto the stack is popped and invoked with the argument arg when the thread exits by calling pthread_exit, when the thread acts on a cancellation request, or when the thread calls pthread_cleanup_pop with a nonzero execute argument.

References: 5.3 Headers: <pthread.h>

Hint: pthread_cleanup_push and pthread_cleanup_pop must be paired

in the same lexical scope.

pthread_setcancelstate

int pthread_setcancelstate (

int state,

int *oldstate);

Atomically set the calling thread's cancelability state to state and return the previous cancelability state at the location referenced by oldstate.

state, oldstate

PTHREAD_CANCEL_ENABLE

Cancellation is enabled.

PTHREAD_CANCEL_DISABLE

Cancellatlon is disabled.

References: 5.3

Headers: <pthread.h>

Errors: [EINVAL]stateisinvalid.

Hint: Use to disable cancellation around "atomic" code that includes can-

cellation points.

pthread_setcanceltype

int pthread_setcanceltype (

int type,

int *oldtype);

Atomically set the calling thread's cancelability type to type and return the previous cancelability type at the location referenced by oldtype.

type, oldtype

PTHREAD_CANCEL_DEFERRED

Only deferred cancellation is allowed.

PTHREAD_CANCEL_ASYNCHRONOUS Asynchronous cancellation is

allowed.

References: 5.3

Headers: <pthread.h>

Errors: [EINVAL] type is invalid.

Hint: Use with caution — most code is not safe for use with asynchronous

cancelability type.

pthread_testcancel

void pthread_testcancel (void);

Creates a deferred cancellation point in the calling thread. The call has no effect if the current cancelability state is PTHREAD_CANCEL_DISABLE.

References: 5.3 Headers: <pthread.h>

Hint: Cancellation is asynchronous. Use pthread_join to wait for termi-

nation of thread if necessary.

Оглавление книги


Генерация: 0.047. Запросов К БД/Cache: 0 / 3
поделиться
Вверх Вниз