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

36-42 Threads are created with an attributes object set to create threads detached, rather than joinable. The result is that threads will cease to exist as soon as they terminate, rather than remaining until main calls pthread_join. The pthread_kill function does not necessarily fail if you attempt to send a signal to a terminated thread (the standard is silent on this point), and you may be merely setting a pending signal in a thread that will never be able to act on it. If this were to occur, the thd_suspend routine would hang waiting for the thread to respond. Although pthread_kill may not fail when sending to a terminated thread, it will fail when sending to a thread that doesn't exist — so this attribute converts a possible hang, when the program is run with ITERATIONS set too low, into an abort with an error message.

51-85 The main thread sleeps for two seconds after creating the threads to allow them to reach a "steady state." It then loops through the first half of the threads, suspending each of them. It waits an additional two seconds and then resumes each of the threads it had suspended. It waits another two seconds, suspends each of the remaining threads (the second half), and then after another two seconds resumes them.

By watching the status messages printed by the individual threads, you can see the pattern of output change as the threads are suspended and resumed.

■ susp.c  part 5 sample program

1 static void *

2 thread_routine (void *arg)

3 {

4 int number = (int)arg;

5 int status;

6 int i;

7 char buffer[128] ;

8

9 for (i = 1; i <= iterations; i++) {

10 /*

11 * Every time each thread does 5000 interations, print

12 * a progress report.

13 */

14 if (i % 2000 == 0) {

15  sprintf (

16  buffer, "Thread %02d: %d\n",

17  number, i);

18  write (1, buffer, strlen (buffer));

19 }

20

21 sched_yield ();

22 }

23

24 return (void *)0;

25 }

26

27 int

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

29 {

30 pthread_t threads[THREAD_COUNT];

31 pthread_attr_t detach;

32 int status;

33 void *result;

34 int i;

35

36 status = pthread_attr_init (&detach);

37 if (status != 0)

38  err_abort (status, "Init attributes object");

39 status = pthread_attr_setdetachstate (

40  &detach, PTHREAD_CREATE_DETACHED);

41 if (status != 0)

42  err_abort (status, "Set create-detached"); 43

44 for (i = 0; i< THREAD_COUNT; i++) {

45  status = pthread_create (

46  &threads[i], &detach, thread_routine, (void *)i);

47  if (status != 0)

48  err_abort (status, "Create thread");

49 }

50

51 sleep (2);

52

53 for (i = 0; i < THREAD_COUNT/2; i++) {

54  printf ("Suspending thread %d.\n", i);

55  status = thd_suspend (threads[i]);

56  if (status != 0)

57  err_abort (status, "Suspend thread");

58 }

59

60 printf ("Sleeping ...\n");

61 sleep (2);

62

63 for (i = 0; i < THREAD_COUNT/2; i++) {

64  printf ("Continuing thread %d.\n", i);

65  status = thd_continue (threads[i]);

66  if (status != 0)

67  err_abort (status, "Suspend thread");

68 }

69

70 for (i = THREAD_COUNT/2; i < THREAD_COUNT; i++) {

71  printf ("Suspending thread %d.\n", i);

72  status = thd_suspend (threads[i]);

73  if (status != 0)

74  err_abort (status, "Suspend thread");

75 }

76

77 printf ("Sleeping ...\n");

78 sleep (2);

79

80 for (i = THREAD_COUNT/2; i < THREAD_COUNT; i++) {

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

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

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

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

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

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