ftrylockfile 中文man頁面
NAME
flockfile, ftrylockfile, funlockfile - 為標(biāo)準(zhǔn)輸入輸出鎖定文件 FILE
SYNOPSIS 總覽
#include <stdio.h> void flockfile(FILE *filehandle);
int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);
DESCRIPTION 描述
標(biāo)準(zhǔn)輸入輸出庫 stdio 函數(shù)是線程安全的。這是通過為每個(gè)文件對象 FILE 賦予一個(gè)鎖定計(jì)數(shù)和 (當(dāng)鎖定計(jì)數(shù)非零時(shí)) 一個(gè)所有者線程來實(shí)現(xiàn)的。對每個(gè)庫函數(shù)調(diào)用,這些函數(shù)等待直到文件對象 FILE 不再被一個(gè)不同的線程鎖定,然后鎖定它,進(jìn)行所需的 I/O 操作,再次對它解鎖。
(注意:這個(gè)鎖定與由函數(shù) flock(2) 和 lockf(3) 實(shí)現(xiàn)的鎖定無關(guān)。)
所有這些操作對 C 程序員來說都是不可見的,但是有兩種理由,需要進(jìn)行更加細(xì)節(jié)的控制。其一,也許某個(gè)線程需要進(jìn)行不可分割的一系列 I/O 操作,不應(yīng)當(dāng)被其他線程的 I/O 所終端。其二,出于效率因素,應(yīng)當(dāng)避免進(jìn)行過多的鎖定來提高效率。
為此,一個(gè)線程可以顯式地鎖定文件對象 FILE,接著進(jìn)行它的一系列 I/O 操作,然后解鎖。這樣可以避免其他線程干擾。如果這樣做的原因是需要達(dá)到更高的效率,應(yīng)當(dāng)使用 stdio 函數(shù)的非鎖定版本來進(jìn)行 I/O 操作:使用 getc_unlocked() 和 putc_unlocked() 來代替 getc() 和 putc()。
函數(shù) flockfile() 等待 *filehandle 不再被其他的線程鎖定,然后使當(dāng)前線程成為 *filehandle 的所有者,然后增加鎖定計(jì)數(shù) lockcount。
函數(shù) funlockfile() 減少鎖定計(jì)數(shù)。
函數(shù) ftrylockfile() 是 flockfile() 的非鎖定版本。它在其他線程擁有 *filehandle 時(shí)不做任何處理,否則取得所有權(quán)并增加鎖定計(jì)數(shù)。
RETURN VALUE 返回值
函數(shù) ftrylockfile() 返回零,如果成功的話 (獲得了鎖定);如果失敗就返回非零。
ERRORS
無。
AVAILABILITY
這些函數(shù)當(dāng)定義了 _POSIX_THREAD_SAFE_FUNCTIONS 時(shí)可用。它們存在于 libc 5.1.1 之后的 libc 版本中,以及 glibc 2.0 之后的 glibc 版本中
CONFORMING TO 標(biāo)準(zhǔn)參考
POSIX.1
SEE ALSO 參見
unlocked_stdio(3)
#p#
NAME
flockfile, ftrylockfile, funlockfile - lock FILE for stdio
SYNOPSIS
#include <stdio.h> void flockfile(FILE *filehandle);
int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);
DESCRIPTION
The stdio functions are thread-safe. This is achieved by assigning to each FILE object a lockcount and (if the lockcount is nonzero) an owning thread. For each library call, these functions wait until the FILE object is no longer locked by a different thread, then lock it, do the requested I/O, and unlock the object again.
(Note: this locking has nothing to do with the file locking done by functions like flock(2) and lockf(3).)
All this is invisible to the C-programmer, but there may be two reasons to wish for more detailed control. On the one hand, maybe a series of I/O actions by one thread belongs together, and should not be interrupted by the I/O of some other thread. On the other hand, maybe the locking overhead should be avoided for greater efficiency.
To this end, a thread can explicitly lock the FILE object, then do its series of I/O actions, then unlock. This prevents other threads from coming in between. If the reason for doing this was to achieve greater efficiency, one does the I/O with the non-locking versions of the stdio functions: with getc_unlocked() and putc_unlocked() instead of getc() and putc().
The flockfile() function waits for *filehandle to be no longer locked by a different thread, then makes the current thread owner of *filehandle, and increments the lockcount.
The funlockfile() function decrements the lock count.
The ftrylockfile() function is a non-blocking version of flockfile(). It does nothing in case some other thread owns *filehandle, and it obtains ownership and increments the lockcount otherwise.
RETURN VALUE
The ftrylockfile() function returns zero for success (the lock was obtained), and nonzero for failure.
ERRORS
None.
AVAILABILITY
These functions are available when _POSIX_THREAD_SAFE_FUNCTIONS is defined. They are in libc since libc 5.1.1 and in glibc since glibc 2.0.
CONFORMING TO
POSIX.1
SEE ALSO
unlocked_stdio(3)