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

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

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

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

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

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

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