中国开发网: 论坛: 程序员情感CBD: 贴子 637803
Tomorrow: 写了个测试程序,在windows里就有问题。
// 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说的对的。
我来了

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录