请教可以在数个线程中同时对一个文件使用open()函数和read()函数吗
请问可以在数个线程中同时对一个文件使用open()函数和read()函数吗?
请问可以在不同线程中同时对一个文件使用open()函数和read()函数吗?
比如:
又或者:
谢谢!
------解决方案--------------------
都是读则没问题,每个线程维护的是自己的文件句柄和文件偏移等信息
------解决方案--------------------
句柄和指针都可以有多个,读的时候可以同时读,但是写的时候要注意互斥,一次只能有一个进行写操作,并且写的时候不能读。这些可以使用mutex实现。
------解决方案--------------------
理论上是可以的,注意同步
------解决方案--------------------
可以啊,但需要同步,不是有文件锁吗? 和线程同步类似
------解决方案--------------------
都可以...
------解决方案--------------------
这样写数据会乱的吧。
同步不好做
------解决方案--------------------
仅供参考:
_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.
请问可以在不同线程中同时对一个文件使用open()函数和read()函数吗?
比如:
//Thread1:
int a = open("c:/1.txt",O_RDONLY);
//Thread2:
int a = open("c:/1.txt",O_RDONLY);
又或者:
int main()
{
int a = open("c:/1.txt",O_RDONLY);
}
//Thread1:
read(a,ptr,100);
//Thread2:
read(a,ptr,100);
谢谢!
------解决方案--------------------
都是读则没问题,每个线程维护的是自己的文件句柄和文件偏移等信息
------解决方案--------------------
句柄和指针都可以有多个,读的时候可以同时读,但是写的时候要注意互斥,一次只能有一个进行写操作,并且写的时候不能读。这些可以使用mutex实现。
------解决方案--------------------
理论上是可以的,注意同步
------解决方案--------------------
可以啊,但需要同步,不是有文件锁吗? 和线程同步类似
------解决方案--------------------
都可以...
------解决方案--------------------
这样写数据会乱的吧。
同步不好做
------解决方案--------------------
仅供参考:
_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.