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

ANSI C provides functions to get and put single characters efficiently into stdio buffers. The functions getchar and putchar operate on stdin and stdout, respectively, and getc and putc can be used on any stdio file stream. These are traditionally implemented as macros for maximum performance, directly reading or writing the file stream's data buffer. Pthreads, however, requires these functions to lock the stdio stream data, to prevent code from accidentally corrupting the stdio buffers.

The overhead of locking and unlocking mutexes will probably vastly exceed the time spent performing the character copy, so these functions are no longer high performance. Pthreads could have defined new functions that provided the locked variety rather than redefining the existing functions; however, the result would be that existing code would be unsafe for use in threads. The working group decided that it was preferable to make existing code slower, rather than to make it incorrect.

Pthreads adds new functions that replace the old high-performance macros with essentially the same implementation as the traditional macros. The functions getc_unlocked, putc_unlocked, getchar_unlocked, and putchar_unlocked do not perform any locking, so you must use flockfile and funlockfile around any sequence of these operations. If you want to read or write a single character, you should usually use the locked variety rather than locking the file stream, calling the new unlocked get or put function, and then unlocking the file stream.

If you want to perform a sequence of fast character accesses, where you would have previously used getchar and putchar, you can now use getchar_unlocked and putchar_unlocked. The following program, putchar.c, shows the difference between using putchar and using a sequence of putchar_unlocked calls within a file lock.

9-20 When the program is run with a nonzero argument or no argument at all, it creates threads running the lock_routine function. This function locks the stdout file stream, and then writes its argument (a string) to stdout one character at a time using putchar_unlocked.

28-37 When the program is run with a zero argument, it creates threads running the unlock_routine function. This function writes its argument to stdout one character at a time using putchar. Although putchar is internally synchronized to ensure that the stdio buffer is not corrupted, the individual characters may appear in any order.

putchar.c

1 #include

2 #include "errors.h"

3

4 /*

5 * This function writes a string (the function's arg) to stdout,

6 * by locking the file stream and using putchar_unlocked to write

7 * each character individually.

8 */

9 void *lock_routine (void *arg)

10 {

11 char *pointer;

12

13 flockfile (stdout);

14 for (pointer = arg; *pointer != '\0'; pointer++) {

15  putchar_unlocked (*pointer);

16  sleep (1);

17 }

18 funlockfile (stdout);

19 return NULL;

20 }

21

22 /*

23 * This function writes a string (the function's arg) to stdout,

24 * by using putchar to write each character individually.

25 * Although the internal locking of putchar prevents file stream

26 * corruption, the writes of various threads may be interleaved.

27 */

28 void *unlock_routine (void *arg)

29 {

30 char *pointer;

31

32 for (pointer = arg; *pointer != '\0'; pointer++) {

33  putchar (*pointer);

34  sleep (1);

35 }

36 return NULL;

37 }

38

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

40 {

41  pthread_t thread1, thread2, thread3;

42  int flock_flag = 1;

43  void *(*thread_func)(void *);

44  int status;

45

46  if (argc > 1)

47  flock_flag = atoi (argv[l]);

48  if (flock_flag)

49  thread_func = lock_routine;

50  else

51  thread_func = unlock_routine;

52  status = pthread_create (

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

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

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

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

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

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

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

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

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