Critical_section用法
#include <windows.h>
#include <stdio.h>
struct wo {
int ii;
int jj;
FILE *fp;
};
CRITICAL_SECTION cs;
DWORD WINAPI testout(LPVOID lparam)
{
EnterCriticalSection(&cs);
wo de=*(wo*)lparam;
printf( "%d------%d is running!\r\n ",de.ii,de.jj);
LeaveCriticalSection(&cs);
return 0;
}
void main()
{
InitializeCriticalSection(&cs);
wo wode[110];
int num=0;
HANDLE hThread[110];
for(int i=0;i <10;i++)
{
for(int j=0;j <10;j++,num++)
{
wode[num].ii=i;
wode[num].jj=j;
hThread[num]=CreateThread(NULL,0,testout,(LPVOID)&wode[num],0,NULL);
}
}
WaitForMultipleObjects(num,hThread,TRUE,INFINITE);
for(i=0;i <num;i++)
CloseHandle(hThread);
DeleteCriticalSection(&cs);
return;
}
大家帮忙看下,为什么有时是对的有时不对?
[解决办法]
CloseHandle(hThread);//写错了
少加[i]了
CloseHandle(hThread[i]);