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

31-42 If an alarm is found, it is removed from the list. The current time is retrieved by calling the time function, and it is compared to the requested time for the alarm. If the alarm has already expired, then alarm_thread will set sleep_time to 0. If the alarm has not expired, alarm_thread computes the difference between the current time and the alarm expiration time, and sets sleep_time to that number of seconds.

52-58 The mutex is always unlocked before sleeping or yielding. If the mutex remained locked, then main would be unable to insert a new alarm on the list. That would make the program behave synchronously—the user would have to wait until the alarm expired before doing anything else. (The user would be able to enter a single command, but would not receive another prompt until the next alarm expired.) Calling sleep blocks alarm_thread for the required period of time—it cannot run until the timer expires.

Calling sched_yield instead is slightly different. We'll describe sched_yield in detail later (in Section 5.5.2)—for now, just remember that calling sched_yield will yield the processor to a thread that is ready to run, but will return immediately if there are no ready threads. In this case, it means that the main thread will be allowed to process a user command if there's input waiting—but if the user hasn't entered a command, sched_yield will return immediately.

64-67 If the alarm pointer is not NULL, that is, if an alarm was processed from alarm_list, the function prints a message indicating that the alarm has expired. After printing the message, it frees the alarm structure. The thread is now ready to process another alarm.

alarm_mutex.c part 2 alarm_thread

1 /*

2 * The alarm thread's start routine.

3 */

4 void *alarm_thread (void *arg)

5 {

6 alarm_t *alarm;

7 int sleep_time;

8 time_t now;

9 int status; 10

11 /*

12 * Loop forever, processing commands. The alarm thread will

13 * be disintegrated when the process exits.

14 */

15 while (1) {

16 status = pthread_mutex_lock (&alarm_mutex);

17 if (status != 0)

18 err_abort (status, "Lock mutex");

19 alarm = alarm_list; 20

21 /*

22 * If the alarm list is empty, wait for one second. This

23 * allows the main thread to run, and read another

24 * command. If the list is not empty, remove the first

25 * item. Compute the number of seconds to wait — if the

26 * result is less than 0 (the time has passed), then set

27 * the sleep_time to 0.

28 */

29 if (alarm == NULL)

30 sleep_time = 1;

31 else {

32 alarm_list = alarm->link;

33 now = time (NULL);

34 if (alarm->time <= now)

35  sleep_time = 0;

36 else

37  sleep_time = alarm->time - now;

38 #ifdef DEBUG

39 printf ("[waiting: %d(%d)\"%s\"]\n", alarm->time,

40 sleep_time, alarm->message);

41 #endif

42 }

43

44 /*

45 * Unlock the mutex before waiting, so that the main

46 * thread can lock it to insert a new alarm request. If

47 * the sleep_time is 0, then call sched_yield, giving

48 * the main thread a chance to run if it has been

49 * readied by user input, without delaying the message

50 * if there's no input.

51 */

52 status = pthread_mutex_unlock (&alarm_mutex);

53 if (status != 0)

54  err_abort (status, "Unlock mutex");

55 if (sleep_time > 0)

56  sleep (sleep_time);

57 else

58  sched_yield ();

59

60 /*

61 * If a timer expired, print the message and free the

62 * structure.

63 */

64 if (alarm != NULL) {

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

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

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

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

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

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

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

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

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

Все жанры