Linux/c++ 多线程写文件时怎么正确使用flockfile/funlockfile
Linux/c++ 多线程写文件时如何正确使用flockfile/funlockfile
最近在一个多线程文件上传服务里想做一个文件锁控制多用户同时写入同样的文件而产生冲突。但是我在一个线程里使用flockfile锁住打开的文件句柄进行写动作的同时,启动另外一个线程打开相同的文件的句柄还是可以获得文件锁并进行写入操作,本来以为第二个线程使用ftrylockfile会失败的,有点搞不清状况。希望有高手指点一下这几个函数的用法。谢谢
------解决方案--------------------
一个线程锁住了,另一个线程肯定访问失败,你的要求是冲突的,一要锁,二要并发写,如果要并发写,锁它干什么?
------解决方案--------------------
仅供参考:
Windows下:
_locking
Locks or unlocks bytes of a file.
int _locking( int handle, int mode, long nbytes );
Routine Required Header Optional Headers Compatibility
_locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:
EACCES
Locking violation (file already locked or unlocked).
EBADF
Invalid file handle.
EDEADLOCK
Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.
EINVAL
An invalid argument was given to _locking.
Parameters
handle
File handle
mode
Locking action to perform
nbytes
Number of bytes to lock
Remarks
The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.
mode must be one of the following manifest constants, which are defined in LOCKING.H:
_LK_LOCK
Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
_LK_NBLCK
最近在一个多线程文件上传服务里想做一个文件锁控制多用户同时写入同样的文件而产生冲突。但是我在一个线程里使用flockfile锁住打开的文件句柄进行写动作的同时,启动另外一个线程打开相同的文件的句柄还是可以获得文件锁并进行写入操作,本来以为第二个线程使用ftrylockfile会失败的,有点搞不清状况。希望有高手指点一下这几个函数的用法。谢谢
------解决方案--------------------
一个线程锁住了,另一个线程肯定访问失败,你的要求是冲突的,一要锁,二要并发写,如果要并发写,锁它干什么?
------解决方案--------------------
仅供参考:
Windows下:
_locking
Locks or unlocks bytes of a file.
int _locking( int handle, int mode, long nbytes );
Routine Required Header Optional Headers Compatibility
_locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:
EACCES
Locking violation (file already locked or unlocked).
EBADF
Invalid file handle.
EDEADLOCK
Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.
EINVAL
An invalid argument was given to _locking.
Parameters
handle
File handle
mode
Locking action to perform
nbytes
Number of bytes to lock
Remarks
The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.
mode must be one of the following manifest constants, which are defined in LOCKING.H:
_LK_LOCK
Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
_LK_NBLCK