用什么方法解决这个问题呢。
HANDLE hThread [10];
for(int i=0;i<5;i++)
{
hThread[i] = CreateThread( NULL, 0, hThreadProc,NULL, 0, 0 );
Sleep(200);
}
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
HANDLE hFile;
char * pData = "12345678899999";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
CloseHandle(hFile);
Sleep(200);
}
return 0;
}
11 个解决方案
#1
用临界区保护一下不就行了么?
#2
对读写文件部分加锁,设置临界区
#3
最简单的用临界区来同步。
CRITICAL_SECTION g_CS;
在创建线程前初始化 InitializeCriticalSection(&g_CS);
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
HANDLE hFile;
char * pData = "12345678899999";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
EnterCriticalSection( &g_CS ); //进入临界区
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
LeaveCriticalSection( &g_CS );
CloseHandle(hFile);
Sleep(200);
}
return 0;
}
#4
临界区 已经加了,可程序运行一段时间,线程还是会自动退出,不知为什么。
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
EnterCriticalSection( &g_cs );
HANDLE hFile;
char * pData = "1111111111233333333334444444444555555556";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
CloseHandle(hFile);
LeaveCriticalSection( &g_cs );
}
return 0;
}
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
EnterCriticalSection( &g_cs );
HANDLE hFile;
char * pData = "1111111111233333333334444444444555555556";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
CloseHandle(hFile);
LeaveCriticalSection( &g_cs );
}
return 0;
}
#5
CloseHandle(hFile); 为什么会引起线程退出呢?
#6
顶起来
#7
用线程同步方法解决
#8
譬如???
我上面写的不是线程同步嘛?
#9
加日志,跟踪
包括hFile
包括hFile
#10
按说加了临界区是不应有问题的.注意&g_cs 是全局的,每个线程用的都是这个.
如果退出,debug状态下调试应该能弹出异常的,具体跟踪一下吧.
如果退出,debug状态下调试应该能弹出异常的,具体跟踪一下吧.
#11
謝謝大家 結貼
#1
用临界区保护一下不就行了么?
#2
对读写文件部分加锁,设置临界区
#3
最简单的用临界区来同步。
CRITICAL_SECTION g_CS;
在创建线程前初始化 InitializeCriticalSection(&g_CS);
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
HANDLE hFile;
char * pData = "12345678899999";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
EnterCriticalSection( &g_CS ); //进入临界区
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
LeaveCriticalSection( &g_CS );
CloseHandle(hFile);
Sleep(200);
}
return 0;
}
#4
临界区 已经加了,可程序运行一段时间,线程还是会自动退出,不知为什么。
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
EnterCriticalSection( &g_cs );
HANDLE hFile;
char * pData = "1111111111233333333334444444444555555556";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
CloseHandle(hFile);
LeaveCriticalSection( &g_cs );
}
return 0;
}
DWORD WINAPI hThreadProc( PVOID pParameter )
{
while(1)
{
EnterCriticalSection( &g_cs );
HANDLE hFile;
char * pData = "1111111111233333333334444444444555555556";
hFile=CreateFile("F:\\12.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
cout<<"create file error"<<endl;
}
DWORD dwWritten;
SetFilePointer(hFile,0,NULL,FILE_END);
if(!WriteFile(hFile,pData,strlen(pData),&dwWritten,NULL))
{
TRACE("write file error\n");
}
else
{
TRACE("write success\n");
}
CloseHandle(hFile);
LeaveCriticalSection( &g_cs );
}
return 0;
}
#5
CloseHandle(hFile); 为什么会引起线程退出呢?
#6
顶起来
#7
用线程同步方法解决
#8
譬如???
我上面写的不是线程同步嘛?
#9
加日志,跟踪
包括hFile
包括hFile
#10
按说加了临界区是不应有问题的.注意&g_cs 是全局的,每个线程用的都是这个.
如果退出,debug状态下调试应该能弹出异常的,具体跟踪一下吧.
如果退出,debug状态下调试应该能弹出异常的,具体跟踪一下吧.
#11
謝謝大家 結貼