Читаем 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.

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

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

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

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

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

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