48 status = pthread_join (thread_id, &result);

49 if (status != 0)

50 err_abort (status, 'Join thread');

51 if (result == PTHREAD_CANCELED)

52 printf ('Thread canceled at iteration %d ', counter);

53 else

54 printf ('Thread was not canceled ');

55 return 0;

56 }

A thread can disable cancellation around sections of code that need to complete without interruption, by calling pthread_setcancelstate. For example, ifa database update operation takes two separate write calls, you wouldn't want to complete the first and have the second canceled. If you request that a thread be canceled while cancellation is disabled, the thread remembers that it was canceled but won't do anything about it until after cancellation is enabled again. Because enabling cancellation isn't a cancellation point, you also need to test for a pending cancel request if you want a cancel processed immediately.

When a thread may be canceled while it holds private resources, such as a locked mutex or heap storage that won't ever be freed by any other thread, those resources need to be released when the thread is canceled. If the thread has a mutex locked, it may also need to 'repair' shared data to restore program invariants. Cleanup handlers provide the mechanism to accomplish the cleanup, somewhat like process atexit handlers. After acquiring a resource, and before any cancellation points, declare a cleanup handler by calling pthread_cleanup_ push. Before releasing the resource, but after any cancellation points, remove the cleanup handler by calling pthread_cleanup_pop.

If you don't have a thread's identifier, you can't cancel the thread. That means that, at least using portable POSIX functions, you can't write an 'idle thread killer' that will arbitrarily terminate threads in the process. You can only cancel threads that you created, or threads for which the creator (or the thread itself) gave you an identifier. That generally means that cancellation is restricted to operating within a subsystem.

5.3.1 Deferred cancelability

'Deferred cancelability' means that the thread's cancelability type has been set to PTHREAD_CANCEL_DEFERRED and the thread's cancelability enable has been set to PTHREAD_CANCEL_ENABLE. The thread will only respond to cancellation requests when it reaches one of a set of 'cancellation points.'

The following functions are always cancellation points on any Pthreads system:

pthread_cond_wait  fsync

pthread_cond_timedwait mq_receive pthread_join mq_send

pthread_testcancel msync

sigwait nanosleep

aio_suspend open

close pause

creat read

fcntl (F_SETLCKW) sem_wait

The following list of functions may be cancellation points. You should write your code so that it will function correctly if any of these are cancellation points

sigwaitinfo

sigsuspend

sigtimedwait

sleep

system

tcdrain

wait

waitpid

write

and also so that it will not break if any of them are not. If you depend upon any particular behavior, you may limit the portability of your code. You'll have to look at the conformance documentation to find out which, if any, are cancellation points for the system you are using:

closedir getc_unlocked printf
ctermid getchar putc
fclose getchar_unlocked putc_unlocked
fcntl (except F_SETLCKW) getcwd putchar
fflush getgrgid putchar_unlocked
fgetc getgrgid_r puts
fgets getrtnam readdir
fopen getgrnam_r remove
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату