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

61  return NULL;

62 }

63

64 int main (int argc, char *argv[])

65 {

66  pthread_t thread_id[THREADS];

67  int count;

68  void *result;

69  int status;

70

71 for (count = 0; count < THREADS; count++) {

72  status = pthread_create (

73  &thread_id[count], NULL, thread_routine, NULL);

74  if (status != 0)

75  err_abort (status, "Create thread");

76 }

77

78 sleep (2);

79

80 for (count = 0; count < THREADS; count++) {

81  status = pthread_cancel (thread_id[count]);

82 if (status != 0)

83  err_abort (status, "Cancel thread"); 84

85 status = pthread_join (thread_id[count], &result);

86 if (status != 0)

87  err_abort (status, "Join thread");

88 if (result == PTHREAD_CANCELED)

89  printf ("thread %d canceled\n", count);

90 else

91  printf ("thread %d was not canceled\n", count);

92 }

93 return 0;

If one of your threads creates a set of threads to "subcontract" some function, say, a parallel arithmetic operation, and the "contractor" is canceled while the function is in progress, you probably won't want to leave the subcontractor threads running. Instead, you could "pass on" the cancellation to each subcontrator thread, letting them handle their own termination independently.

If you had originally intended to join with the subcontractors, remember that they will continue to consume some resources until they have been joined or detached. When the contractor thread cancels them, you should not delay cancellation by joining with the subcontractors. Instead, you can cancel each thread and immediately detach it using pthread_detach. The subcontractor resources can then be recycled immediately as they finish, while the contractor can wrap things up independently.

The following program, cancel_subcontract.c, shows one way to propagate cancellation to subcontractors.

9-12 The team_t structure defines the state of the team of subcontractor threads. The join_i member records the index of the last subcontractor with which the contractor had joined, so on cancellation from within pthread_join, it can cancel the threads it had not yet joined. The workers member is an array recording the thread identifiers of the subcontractor threads.

18-25 The subcontractor threads are started running the worker_routine function. This function loops until canceled, calling pthread_testcancel every 1000 iterations.

31-46 The cleanup function is established as the active cleanup handler within the contractor thread. When the contractor is canceled, cleanup iterates through the remaining (unjoined) subcontractors, cancelling and detaching each. Note that it does not join the subcontractors — in general, it is not a good idea to wait in a cleanup handler. The thread, after all, is expected to clean up and terminate, not to wait around for something to happen. But if your cleanup handler really needs to wait for something, don't be afraid, it will workjust fine.

53-76 The contractor thread is started running thread_routine. This function creates a set of subcontractors, then joins with each subcontractor. As it joins each thread, it records the current index within the workers array in the team_t member join_i. The cleanup handler is established with a pointer to the team structure so that it can determine the last offset and begin cancelling the remaining subcontractors.

78-104 The main program creates the contractor thread, running thread_routine, and then sleeps for five seconds. When it wakes up, it cancels the contractor thread, and waits for it to terminate.

cancel_subcontract.c

1 #include

2 #include "errors.h" 3

4 #define THREADS 5

5

6 /*

7 * Structure that defines the threads in a "team."

8 */

9 typedef struct team_tag {

10  int join_i; /* join index */

11  pthread_t workers[THREADS]; /* thread identifiers */

12 } team_t;

13

14 /*

15 * Start routine for worker threads. They loop waiting for a

16 * cancellation request.

17 */

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

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

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

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