Читаем Programming with POSIX® Threads полностью

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:

closedirgetc_unlockedprintf
ctermidgetcharputc
fclosegetchar_unlockedputc_unlocked
fcntl (except F_SETLCKW)getcwdputchar
fflushgetgrgidputchar_unlocked
fgetcgetgrgid_rputs
fgetsgetrtnamreaddir
fopengetgrnam_rremove
fprintfgetloginrename
fputcgetlogin_rrewind
fputsgetpwnamrewinddir
freadgetpwnam_rscanf
freopengetpwuidtmpfile
fscanfgetpwuid_rtmpname
fseekgetsttyname
ftelllseekttyname_r
fwriteopendirungetc
getcperror

Pthreads specifies that any ANSI C or POSIX function not specified in one of the two lists cannot be a cancellation point. However, your system probably has many additional cancellation points. That's because few UNIX systems are "POSIX." That is, they support other programming interfaces as well — such as BSD 4.3, System V Release 4, UNLX95, and so forth. POSIX doesn't recognize the existence of functions such as select or poll, and therefore it can't say whether or not they are cancellation points. Yet clearly both are functions that may block for an arbitrary period of time, and programmers using them with cancellation would reasonably expect them to behave as cancellation points. X/Open is currently addressing this problem for UNLX98 (X/Open System Interfaces, Issue 5). by extending the Pthreads list of cancellation points.

Most cancellation points involve I/O operations that may block the thread for an "unbounded" time. They're cancelable so that the waits can be interrupted. When a thread reaches a cancellation point the system determines whether a cancel is pending for the current ("target") thread. A cancel will be pending if another thread has called pthread_cancel for the target thread since the last time the target thread returned from a cancellation point. If a cancel is pending, the system will immediately begin calling cleanup functions, and then the thread will terminate.

If no cancel is currently pending, the function will proceed. If another thread requests that the thread be canceled while the thread is waiting for something (such as I/O) then the wait will be interrupted and the thread will begin its cancellation cleanup.

If you need to ensure that cancellation can't occur at a particular cancellation point, or during some sequence of cancellation points, you can temporarily disable cancellation in that region of code. The following program, called cancel_ disable.c, is a variant of cancel.c. The "target" thread periodically calls sleep, and does not want the call to be cancelable.

23-32 After each cycle of 755 iterations, thread_routine will call sleep to wait a second. (The value 755 is just an arbitrary number that popped into my head. Do arbitrary numbers ever pop into your head?) Prior to sleeping, thread_routine disables cancellation by setting the cancelability state to PTHREAD_CANCEL_ DISABLE. After sleep returns, it restores the saved cancelability state by calling pthread_setcancelstate again.

33-35 Just as in cancel.c, test for a pending cancel every 1000 iterations.

cancel_disable.c

1 #include

2 #include "errors.h"

3

4 static int counter;

5

6 /*

7 * Thread start routine.

8 */

9 void *thread_routine (void *arg)

10 {

11 int state;

12 int status;

13

14 for (counter = 0; ; counter++) {

15

16 /*

17 * Each 755 iterations, disable cancellation and sleep

18 * for one second.

19 *

20 * Each 1000 iterations, test for a pending cancel by

21 * calling pthread_testcancel().

22 */

23 if ((counter % 755) == 0) {

24 status = pthread_setcancelstate (

25 PTHREAD_CANCEL_DISABLE, &state);

26 if (status != 0)

Перейти на страницу:

Похожие книги

C++: базовый курс
C++: базовый курс

В этой книге описаны все основные средства языка С++ - от элементарных понятий до супервозможностей. После рассмотрения основ программирования на C++ (переменных, операторов, инструкций управления, функций, классов и объектов) читатель освоит такие более сложные средства языка, как механизм обработки исключительных ситуаций (исключений), шаблоны, пространства имен, динамическая идентификация типов, стандартная библиотека шаблонов (STL), а также познакомится с расширенным набором ключевых слов, используемым в .NET-программировании. Автор справочника - общепризнанный авторитет в области программирования на языках C и C++, Java и C# - включил в текст своей книги и советы программистам, которые позволят повысить эффективность их работы. Книга рассчитана на широкий круг читателей, желающих изучить язык программирования С++.

Герберт Шилдт

Программирование, программы, базы данных