Книга: Programming with POSIX® Threads

6.4.2 getchar unlocked and putchar unlocked

6.4.2 getchar unlocked and putchar unlocked

int getc_unlocked (FILE *stream);

int getchar_unlocked (void);

int putc_unlocked (int c, FILE *stream);

int putchar_unlocked (int c);

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 <pthread.h>
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 != ''; 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 != ''; 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 (
53  &thread1, NULL, thread_func, "this is thread 1n");
54  if (status != 0)
55  err_abort (status, "Create thread");
56  status = pthread_create (
57  &thread2, NULL, thread_func, "this is thread 2n");
58  if (status != 0)
59  err_abort (status, "Create thread");
60  status = pthread_create (
61  &thread3, NULL, thread_func, "this is thread 3n");
62  if (status != 0)
63  err_abort (status, "Create thread");
64  pthread_exit (NULL);
65 }

Оглавление книги


Генерация: 1.409. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз