[阅读: 381] 2008-06-02 11:00:48
// mttest.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
static HANDLE hSetThrd,hGetThrd;
UINT32 g_nCount = 200;
static unsigned long set_tid,get_tid;
DWORD WINAPI set_thread(void* arg)
{
for(;;)
{
g_nCount++;
g_nCount--;
}
return 0;
}
DWORD WINAPI get_thread(void* arg)
{
for(;;)
{
if((g_nCount == 200) || (g_nCount == 201) || (g_nCount == 199))
{
}else{
printf("failed.g_nCount:%u.",g_nCount);
break;
}
}
return 0;
}
int main()
{
hGetThrd = CreateThread(NULL, 0, get_thread, NULL, 0, &get_tid);
hSetThrd = CreateThread(NULL, 0, set_thread, NULL, 0, &set_tid);
for(;;)
{
Sleep(1000);
};
return 1;
}
在get_thread里面会进入else,但是打印的数据是对的。说明在判断时取的数据不对。
bjwf说的对的。
我来了