1flockfile(3C) Standard C Library Functions flockfile(3C)
2
3
4
6 flockfile, funlockfile, ftrylockfile - acquire and release stream lock
7
9 #include <stdio.h>
10
11 void flockfile(FILE *stream);
12
13
14 void funlockfile(FILE *stream);
15
16
17 int ftrylockfile(FILE *stream);
18
19
21 The flockfile() function acquires an internal lock of a stream stream.
22 If the lock is already acquired by another thread, the thread calling
23 flockfile() is suspended until it can acquire the lock. In the case
24 that the stream lock is available, flockfile() not only acquires the
25 lock, but keeps track of the number of times it is being called by the
26 current thread. This implies that the stream lock can be acquired more
27 than once by the same thread.
28
29
30 The funlockfile() function releases the lock being held by the current
31 thread. In the case of recursive locking, this function must be called
32 the same number of times flockfile() was called. After the number of
33 funlockfile() calls is equal to the number of flockfile() calls, the
34 stream lock is available for other threads to acquire.
35
36
37 The ftrylockfile() function acquires an internal lock of a stream
38 stream, only if that object is available. In essence ftrylockfile() is
39 a non-blocking version of flockfile().
40
42 The ftrylockfile() function returns 0 on success and non-zero to indi‐
43 cate a lock cannot be acquired.
44
46 Example 1 A sample program of flockfile().
47
48
49 The following example prints everything out together, blocking other
50 threads that might want to write to the same file between calls to
51 fprintf(3C):
52
53
54 FILE iop;
55 flockfile(iop);
56 fprintf(iop, "hello ");
57 fprintf(iop, "world);
58 fputc(iop, 'a');
59 funlockfile(iop);
60
61
62
63 An unlocked interface is available in case performance is an issue.
64 For example:
65
66
67 flockfile(iop);
68 while (!feof(iop)) {
69 *c++ = getc_unlocked(iop);
70 }
71 funlockfile(iop);
72
73
75 See attributes(5) for descriptions of the following attributes:
76
77
78
79
80 ┌─────────────────────────────┬─────────────────────────────┐
81 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
82 ├─────────────────────────────┼─────────────────────────────┤
83 │Interface Stability │Standard │
84 ├─────────────────────────────┼─────────────────────────────┤
85 │MT-Level │MT-Safe │
86 └─────────────────────────────┴─────────────────────────────┘
87
89 Intro(3), __fsetlocking(3C), ferror(3C), fprintf(3C), getc(3C),
90 putc(3C), stdio(3C), ungetc(3C), attributes(5), standards(5)
91
93 The interfaces on this page are as specified in IEEE Std 1003.1:2001.
94 See standards(5).
95
96
97
98SunOS 5.11 10 Sep 2003 flockfile(3C)