[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    flockfile(3S) manual page Table of Contents

Name

flockfile, funlockfile, ftrylockfile - acquire and release stream lock

Synopsis

#include <stdio.h>

void flockfile(FILE *stream);

void funlockfile(FILE *stream);

int ftrylockfile(FILE *stream);

MT-Level

MT-Safe

Description

flockfile() acquires an internal lock of a stream stream. If the lock is already acquired by another thread, the thread calling flockfile() is suspended until it can acquire the lock. In the case that the stream lock is available, flockfile() not only acquires the lock, but keeps track of the number of times it is being called by the current thread. This implies that the stream lock can be acquired more than once by the same thread.

funlockfile() releases the lock being held by the current thread. In the case of recursive locking, this function must be called the same number of times flockfile() was called. After the number of funlockfile() calls is equal to the number of flockfile() calls, the stream lock is available for other threads to acquire.

ftrylockfile() acquires an internal lock of a stream stream, only if that object is available. In essence ftrylockfile() is a non-blocking version of flockfile().

Return Values

ftrylockfile() returns zero on success or non-zero to indicate a lock cannot be acquired.

Examples

This example prints everything out together, blocking other threads that might want to write to the same file between fprintf()s:


FILE iop;

flockfile(iop);
fprintf(iop, "hello ");
fprintf(iop, "world0);
fputc(iop, ’a’);
funlockfile(iop);

An unlocked interface is available in case performance is an issue. For example:


flockfile(iop);
while (!feof(iop)) {
    *c++ = getc_unlocked(iop);
}
funlockfile(iop);

See Also

intro(3) , ferror(3S) , getc(3S) , putc(3S) , stdio(3S) , ungetc(3S)

Notes

The interfaces on this page are as specified in the POSIX 1003.1c standard.


Table of Contents