急!!!多线程对文件操作,高高手

hyq03 2007-04-12 11:28:14
本我采用unicode格式,写入日志文件
多线程里经常调用这个函数
日志文件中加入了临界区,同步
一秒钟可能向文件操作N次,不同的线程.频繁open()->write()->close()
执行2分钟后,DEBUG出现异常
错误提示为File *fp.(stream !=NULL),fwrite.c文件
还有这种提示fputwc函数 (str!=NULL) fputws.c文件
-------------------------------------------------------
有没有高手解决呀!什么原因会这样呀,都加了同步控制
...全文
971 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
wsd2616412 2007-04-13
  • 打赏
  • 举报
回复
mark
vcayi 2007-04-12
  • 打赏
  • 举报
回复
更正一下上面的做法

进临界区
if(fp ==NULL)
{
open()文件

}

write()文件

退出临界区


vcayi 2007-04-12
  • 打赏
  • 举报
回复
没有必要每次操作文件都先打开文件再写文件再关闭文件
可以定义一个你要写文件的所有线程都可以访问的File *fp指针
然后当线程要写文件时就先判断fp指针是否为空如果是就open文件,如果不是证明已经打开了
就不用再打开文件,直接进临界区->write文件->退出临近区,程序退出时再close文件
这样效率就提高了而且应该估计也会解决你说的异常问题
WingForce 2007-04-12
  • 打赏
  • 举报
回复
初始化临界
进入临界
打开文件-》写入文件-》关闭文件
临界推出
======================================
...这个效率...
wangzhangyong411 2007-04-12
  • 打赏
  • 举报
回复
确定同步没加错吗?

CreateMutex(NULL, FALSE, "My Mutex");

WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED;

ReleaseMutex(hMutex);

CloseHandle(hMutex);

估计会是同步上的问题


代码帖出点看看嘛


WingForce 2007-04-12
  • 打赏
  • 举报
回复
为什么要一秒钟操作n次捏?
先写到一个缓存里,积累到一定量的时候再写文件

另外,不是说加了临界区就怎么怎么了,有可能加错了
lockhall 2007-04-12
  • 打赏
  • 举报
回复
re...
WingForce 2007-04-12
  • 打赏
  • 举报
回复
再说2句:
线程,进程的出现并不是为了提高运行效率的,它们根本就只会降低运行效率,没有调度的系统肯定比调度的系统快很多
提出线程和进程的概念完全是为了软件工程,可以提供一个方便的开发平台,分离各个功能模块的耦合度
WingForce 2007-04-12
  • 打赏
  • 举报
回复
如果多线程中只是读文件或写文件,没有其它操作.那么,用多线程绝对是种错误的理念.

用了多线程,那么假设文件在一个线程中正在写,这时你要控制文件在另一个线程中不能读写.只有先那个线程写操作完毕,另一个线程才能读写,这跟在一个线程中调用两次读写有何区别.反而增加了线程的开销.
========================================================================================
太绝对了
典型的例子就是一个银行系统,它需要给很多用户提供服务,所以会为每个用户启动一个线程
同时它也要访问数据库,相当于读写文件,而可以近似的看做它在独占的写入
0黄瓜0 2007-04-12
  • 打赏
  • 举报
回复
如果多线程中只是读文件或写文件,没有其它操作.那么,用多线程绝对是种错误的理念.

用了多线程,那么假设文件在一个线程中正在写,这时你要控制文件在另一个线程中不能读写.只有先那个线程写操作完毕,另一个线程才能读写,这跟在一个线程中调用两次读写有何区别.反而增加了线程的开销.

WingForce 2007-04-12
  • 打赏
  • 举报
回复
The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.

Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program's object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ.

For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen.

WingForce 2007-04-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

CRITICAL_SECTION cs;
FILE* f;

void thread_func( void* ptr )
{
DWORD id = GetCurrentThreadId();
int i;
char tbuf[16];

if( f == NULL )
{
printf( "file is null\n" );
return;
}

for( i = 0; i < 10000; i ++ )
{
EnterCriticalSection( &cs );
printf( "thread<%d> : %s<%d>\n", id, _strtime( tbuf ), i );
fprintf( f, "thread<%d> : %s<%d>\n", id, _strtime( tbuf ), i );
fflush( f );
LeaveCriticalSection( &cs );
}

return ;
}

int main(int argc, char *argv[])
{
int i;
int thandles[10];
if( ( f = fopen( "log.txt", "a+" ) ) == NULL )
{
printf( "fopen failed:%d\n", GetLastError() );
return;
}

InitializeCriticalSection( &cs );

for( i = 0; i < sizeof( thandles ) / sizeof( thandles[0] ); i ++ )
thandles[i] = _beginthread( thread_func, 0, 0 );

WaitForMultipleObjects( sizeof( thandles ) / sizeof( thandles[0] ),
thandles,
1,
60 * 1000 );

fclose( f );

DeleteCriticalSection( &cs );

system("PAUSE");
return 0;

}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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