Posted on 2009-04-05 17:25
S.l.e!ep.¢% 阅读(1114)
评论(0) 编辑 收藏 引用 所属分类:
WinDbg
内存崩溃的BUG 之 另一case 1
查了一下,原来是引用计数里面的 m_count++, 用得有问题!
#include <iostream>
using namespace std;
#include <windows.h>
#include <process.h>
DWORD g_value = 0;
#define NUMBER_COUNT 2000
void __cdecl threadfun(void* pVoid)
{
for (int i = 0; i <= NUMBER_COUNT; i++ )
{
// g_value++;
// g_value--;
InterlockedIncrement((long*)&g_value);
InterlockedDecrement((long*)&g_value);
}
}
int main()
{
int i = 0;
g_value = 1;
HANDLE hArray[NUMBER_COUNT] = {INVALID_HANDLE_VALUE};
for ( i = 0; i <= NUMBER_COUNT; i++ )
{
hArray[i] = (HANDLE)_beginthread(threadfun, 0, (void*)i);
}
for ( i = 0; i <= NUMBER_COUNT; i++ )
{
DWORD dwRet = 0;
DWORD dwExitCode = 0;
if( TRUE == ::GetExitCodeThread(hArray[i], &dwExitCode) )
{
if( STILL_ACTIVE == dwExitCode )
dwRet = ::WaitForSingleObject(hArray[i], INFINITE); // INFINITE
}
}
cout << g_value << endl;
return 0;
}