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

78 array[i] = target_thread;

79

80  status = pthread_mutex_unlock (&mut);

81  return status;

82 }

23-26The thd_continue function first checks whether the suspend/resume package has been initialized (inited is not 0). If it has not been initialized, then no threads are suspended, and thd_continue returns with success.

33-39 If the specified thread identifier is not found in the array of suspended threads, then it is not suspended — again, return with success.

45-51 Send the resume signal, SIGUSR2. There's no need to wait — the thread will resume whenever it can, and the thread calling thd_continue doesn't need to know.

■ susp.c part 4 thd_continue

1 /*

2 * Resume a suspended thread by sending it SIGUSR2 to break

3 * it out of the sigsuspend() in which it's waiting. If the

4 * target thread isn't suspended, return with success.

5 */

6 int

7 thd_continue (pthread_t target_thread)

8 {

9 int status;

10 int i = 0;

11

12 /*

13 * Serialize access to suspend, makes life easier.

14 */

15 status = pthread_mutex_lock (&mut);

16 if (status != 0)

17  return status;

18

19 /*

20 * If we haven't been initialized, then the thread must be

21 * "resumed"; it couldn't have been suspended!

22 */

23 if (!inited) {

24  status = pthread_mutex_unlock (&mut);

25  return status;

26 }

27

28 /*

29 * Make sure the thread is in the suspend array. If not, it

30 * hasn't been suspended (or it has already been resumed) and

31 * we can just carry on.

32 */

33 while (array[i] != target_thread && i < bottom)

34  i++;

35

36 if (i >= bottom) {

37  pthread_mutex_unlock (&mut);

38  return 0;

39 }

40

41 /*

42 * Signal the thread to continue, and remove the thread from

43 * the suspended array.

44 */

45 status = pthread_kill (target_thread, SIGUSR2);

46 if (status != 0) {

47  pthread_mutex_unlock (&mut);

48  return status;

49 }

50

51 array[i] = 0; /* Clear array element */

52 status = pthread_mutex_unlock (&mut);

53 return status;

54 }

2-25 The thread_routine function is the thread start routine for each of the "target" threads created by the program. It simply loops for a substantial period of time, periodically printing a status message. On each iteration, it yields to other threads to ensure that the processor time is apportioned "fairly" across all the threads.

Notice that instead of calling printf, the function formats a message with sprintf and then displays it on stdout (file descriptor 1) by calling write. This illustrates one of the problems with using suspend and resume (thd_suspend and thd_continue) for synchronization. Suspend and resume are scheduling functions, not synchronization functions, and using scheduling and synchronization controls together can have severe consequences.

Incautious use of suspend and resume can deadlock your application.

In this case, if a thread were suspended while modifying a stdio stream, all other threads that tried to modify that stdio stream might block, waiting for a mutex that is locked by the suspended thread. The write function, on the other hand, is usually a call to the kernel — the kernel is atomic with respect to signals, and therefore can't be suspended. Use of write, therefore, cannot cause a deadlock.

In general, you cannot suspend a thread that may possibly hold any resource, if that resource may be required by some other thread before the suspended thread is resumed. In particular, the result is a deadlock if the thread that would resume the suspended thread first needs to acquire the resource. This prohibition includes, especially, mutexes used by libraries you call — such as the mutexes used by malloc and free, or the mutexes used by stdio.

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

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

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

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

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

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

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

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

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