大神救我,多线程同时读写一个文件

小小小小小小白菜 2015-05-21 08:12:28
我想用主线程写东西到一个文件,同时一个线程从同一文件读东西出来
怎么弄?两个文件指针?
...全文
1624 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
小羽1 2016-11-04
  • 打赏
  • 举报
回复
引用 15 楼 shanxuezhe 的回复:
牛刀??那杀鸡刀呢?似乎只有这种方法比较合适【文件映射】 我遇到了和你一样的问题,一个线程不停的写,一个线程不停的读,所以不存在同步的问题,两个线程互相独立,根本不需要同步。 不知道除了文件映射,您还有别的方法吗?求教
亲,这个同一文件两个线程同时读写,用文件映射就可以是吗?新手不知道从哪里下手
小羽1 2016-11-04
  • 打赏
  • 举报
回复
楼主,可以讲一下思路吗?我现在也是想要实现这个
常人changr 2016-02-19
  • 打赏
  • 举报
回复
牛刀??那杀鸡刀呢?似乎只有这种方法比较合适【文件映射】 我遇到了和你一样的问题,一个线程不停的写,一个线程不停的读,所以不存在同步的问题,两个线程互相独立,根本不需要同步。 不知道除了文件映射,您还有别的方法吗?求教
  • 打赏
  • 举报
回复
引用 10 楼 bjym1987 的回复:
[quote=引用 2 楼 luxiaolai861227 的回复:] EnterCriticalSection(&m_lockLogWrite); //释放资源 LeaveCriticalSection(&m_lockLogWrite); 操作文件的时候,用临界区就可以了
参考这个也可以[/quote] 是的,不过我最后用牛刀去杀鸡了
  • 打赏
  • 举报
回复
引用 2 楼 luxiaolai861227 的回复:
EnterCriticalSection(&m_lockLogWrite); //释放资源 LeaveCriticalSection(&m_lockLogWrite); 操作文件的时候,用临界区就可以了
确实,我兜了一个大圈,不过我用那个共享内存的方法就不用考虑同步了,(因为读并不影响数据) 但这个临界区,我还没调成功,所以先放弃了
  • 打赏
  • 举报
回复
引用 11 楼 xian_wwq 的回复:
[quote=引用 7 楼 baidu_27121737 的回复:] [quote=引用 4 楼 xian_wwq 的回复:] 如果没有要求, 也可以一个线程生成文件,写入数据后关闭句柄, 读线程检测到文件后,读取数据,然后删除。
后来我用了共享内存映射,但是,串口来的数写到共享内存里,就是更新不到磁盘文件里去(那个flushviewoffile 返回都是对的可是文件里就是没有东西);看网上说线程用“同步对象”我都糊涂了,咋办??[/quote] 所谓同步,就是对于共有变量或者资源,在操作时进行加锁 临界区、信号量等都可以实现呀[/quote] 谢谢你呀,我用内存映射文件,大概实现了目标(mapviewofffile时参数没设对),我由于是一边读一边写,不考虑同步也可以的,这个方法我就没考虑同步,临界区,互斥等方法我以后再学习吧。
xian_wwq 2015-05-27
  • 打赏
  • 举报
回复
引用 7 楼 baidu_27121737 的回复:
[quote=引用 4 楼 xian_wwq 的回复:] 如果没有要求, 也可以一个线程生成文件,写入数据后关闭句柄, 读线程检测到文件后,读取数据,然后删除。
后来我用了共享内存映射,但是,串口来的数写到共享内存里,就是更新不到磁盘文件里去(那个flushviewoffile 返回都是对的可是文件里就是没有东西);看网上说线程用“同步对象”我都糊涂了,咋办??[/quote] 所谓同步,就是对于共有变量或者资源,在操作时进行加锁 临界区、信号量等都可以实现呀
老王爱上猫 2015-05-27
  • 打赏
  • 举报
回复
引用 2 楼 luxiaolai861227 的回复:
EnterCriticalSection(&m_lockLogWrite); //释放资源 LeaveCriticalSection(&m_lockLogWrite); 操作文件的时候,用临界区就可以了
参考这个也可以
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
_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 Locks the specified bytes. If the bytes cannot be locked, the constant returns an error. _LK_NBRLCK Same as _LK_NBLCK. _LK_RLCK Same as _LK_LOCK. _LK_UNLCK Unlocks the specified bytes, which must have been previously locked. Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program. Example /* LOCKING.C: This program opens a file with sharing. It locks * some bytes before reading them, then unlocks them. Note that the * program works correctly only if the file exists. */ #include <io.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/locking.h> #include <share.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> void main( void ) { int fh, numread; char buffer[40]; /* Quit if can't open file or system doesn't * support sharing. */ fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE ); if( fh == -1 ) exit( 1 ); /* Lock some bytes and read them. Then unlock. */ if( _locking( fh, LK_NBLCK, 30L ) != -1 ) { printf( "No one can change these bytes while I'm reading them\n" ); numread = _read( fh, buffer, 30 ); printf( "%d bytes read: %.30s\n", numread, buffer ); lseek( fh, 0L, SEEK_SET ); _locking( fh, LK_UNLCK, 30L ); printf( "Now I'm done. Do what you will with them\n" ); } else perror( "Locking failed\n" ); _close( fh ); } Output No one can change these bytes while I'm reading them 30 bytes read: /* LOCKING.C: This program ope Now I'm done. Do what you will with them File Handling Routines See Also _creat, _open
那个同步锁吗,我没有学过,好烦啊
  • 打赏
  • 举报
回复
引用 6 楼 tabris17 的回复:
两个文件句柄,一个写一个读,不做同步的话可能会读取到脏数据
后来我用了共享内存映射,但是,串口来的数写到共享内存里,就是更新不到磁盘文件里去(那个flushviewoffile 返回都是对的可是文件里就是没有东西);看网上说线程用“同步对象”我都糊涂了,进程才需要“共享内存”咋办??
  • 打赏
  • 举报
回复
引用 4 楼 xian_wwq 的回复:
如果没有要求, 也可以一个线程生成文件,写入数据后关闭句柄, 读线程检测到文件后,读取数据,然后删除。
后来我用了共享内存映射,但是,串口来的数写到共享内存里,就是更新不到磁盘文件里去(那个flushviewoffile 返回都是对的可是文件里就是没有东西);看网上说线程用“同步对象”我都糊涂了,咋办??
xian_wwq 2015-05-22
  • 打赏
  • 举报
回复
如果没有要求, 也可以一个线程生成文件,写入数据后关闭句柄, 读线程检测到文件后,读取数据,然后删除。
Eleven 2015-05-22
  • 打赏
  • 举报
回复
注意同步问题~~~
处处留心 2015-05-22
  • 打赏
  • 举报
回复
EnterCriticalSection(&m_lockLogWrite); //释放资源 LeaveCriticalSection(&m_lockLogWrite); 操作文件的时候,用临界区就可以了
tabris17 2015-05-22
  • 打赏
  • 举报
回复
两个文件句柄,一个写一个读,不做同步的话可能会读取到脏数据
赵4老师 2015-05-22
  • 打赏
  • 举报
回复
_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 Locks the specified bytes. If the bytes cannot be locked, the constant returns an error. _LK_NBRLCK Same as _LK_NBLCK. _LK_RLCK Same as _LK_LOCK. _LK_UNLCK Unlocks the specified bytes, which must have been previously locked. Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program. Example /* LOCKING.C: This program opens a file with sharing. It locks * some bytes before reading them, then unlocks them. Note that the * program works correctly only if the file exists. */ #include <io.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/locking.h> #include <share.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> void main( void ) { int fh, numread; char buffer[40]; /* Quit if can't open file or system doesn't * support sharing. */ fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE ); if( fh == -1 ) exit( 1 ); /* Lock some bytes and read them. Then unlock. */ if( _locking( fh, LK_NBLCK, 30L ) != -1 ) { printf( "No one can change these bytes while I'm reading them\n" ); numread = _read( fh, buffer, 30 ); printf( "%d bytes read: %.30s\n", numread, buffer ); lseek( fh, 0L, SEEK_SET ); _locking( fh, LK_UNLCK, 30L ); printf( "Now I'm done. Do what you will with them\n" ); } else perror( "Locking failed\n" ); _close( fh ); } Output No one can change these bytes while I'm reading them 30 bytes read: /* LOCKING.C: This program ope Now I'm done. Do what you will with them File Handling Routines See Also _creat, _open
笨笨仔 2015-05-21
  • 打赏
  • 举报
回复
有多个方法可以实现。比如使用一个文件管理线程,通过消息管理写和读等

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧