47  stack_size, PTHREAD_STACK_MIN);

48  status = pthread_attr_setstacksize (

49  &thread_attr, PTHREAD_STACK_MIN*2);

50  if (status != 0)

51  err_abort (status, 'Set stack size');

52 #endif

53  status = pthread_create (

54  &thread_id, &thread_attr, thread_routine, NULL);

55  if (status != 0)

56  err_abort (status, 'Create thread');

57  printf ('Main exiting ');

58  pthread_exit (NULL);

59 return 0;

60 }

5.3 Cancellation

'Now, I give you fair warning,'

shouted the Queen, stamping on the ground as she spoke;

'either you or your head must be off,

and that in about halfno time! Take your choice!'

The Duchess took her choice, and was gone in a moment.

Lewis Carroll,Alice's Adventures in Wonderland

int pthread_cancel (pthread_t thread);

int pthread_setcancelstate (int state, int *oldstate);

int pthread_setcanceltype (int type, int *oldtype);

void pthread_testcancel (void);

void pthread_cleanup_push (

void (*routine)(void *), void *arg);

void pthread_cleanup_pop (int execute);

Most of the time each thread runs independently, finishes a specific job, and exits on its own. But sometimes a thread is created to do something that doesn't necessarily need to be finished. The user might press a CANCEL button to stop a long search operation. Or the thread might be part of a redundant algorithm and is no longer useful because some other thread succeeded. What do you do when you just want a thread to go away? That's what the Pthreads cancellation interfaces are for.

Cancelling a thread is a lot like telling a human to stop something they're doing. Say that one of the bailing programmers has become maniacally obsessed with reaching land, and refuses to stop rowing until reaching safety (Figure 5.1). When the boat finally runs up onto the beach, he's become so fixated that he fails to realize he's done. The other programmers must roughly shake him, and forcibly remove the oars from his blistered hands to stop him — but clearly he must be stopped. That's cancellation. Sort of. I can think of other analogies for cancellation within the bailing programmer story, but I choose to ignore them. Perhaps you can, too.

Cancellation allows you to tell a thread to shut itself down. You don't need it often, but it can sometimes be extremely useful. Cancellation isn't an arbitrary external termination. It is more like a polite (though not necessarily 'friendly') request. You're most likely to want to cancel a thread when you've found that something you set it off to accomplish is no longer necessary. You should never use cancellation unless you really want the target thread to go away. It is a termination mechanism, not a communication channel. So, why would you want to do that to a thread that you presumably created for some reason?

FIGURE 5.1 Thread cancellation analogy

An application might use threads to perform long-running operations, perhaps in the background, while the user continues working. Such operations might include saving a large document, preparing to print a document, or sorting a large list. Most such interfaces probably will need to have some way for the user to cancel an operation, whether it is pressing the ESC key or Ctrl-C, or clicking a stop sign icon on the screen. The thread receiving the user interface cancel request would then determine that one or more background operations were in progress, and use pthread_cancel to cancel the appropriate threads.

Often, threads are deployed to 'explore' a data set in parallel for some heuristic solution. For example, solving an equation for a local minimum or maximum. Once you've gotten one answer that's good enough, the remaining threads may no longer be needed. If so, you can cancel them to avoid wasting processor time and get on to other work.

Pthreads allows each thread to control its own termination. It can restore program invariants and unlock mutexes. It can even defer cancellation while it completes some important operation. For example, when two write operations must both complete if either completes, a cancellation between the two is not acceptable.

Mode State Type Meaning
Off disabled may be either Cancellation remains pending until enabled.
Deferred enabled deferred Cancellation occurs at next cancellation point.
Asynchronous enabled asynchronous _ Cancellation may be processed at any time.

TABLE 5.1 Cancellation states

Pthreads supports three cancellation modes, described in Table 5.1, which are encoded as two binary values called 'cancellation state' and 'cancellation type.' Each essentially can be on or off. (While that technically gives four modes, one of them is redundant.) As shown in the table, cancellation state is said to be

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

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

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