两个线程中的分别去调用同一个类中的PingCount变量的值进行判断,但为什么两个线程中的PingCount总是会一样呢?比如:同時啟動兩個线程,我在一個线程中調用PingTest功能,讀取PingCount
的值,在另一個线程中也調用PingTest功能,讀取PingCount,我知道這兩個pingcount的值是不同的,但這兩個PingCount的值為什么在兩個线程中讀取的时候都一樣呢?應該不同才對呀?
代碼如下:
class PingTest
{public:
int PingCount; /* number of ping operation successful*/
}
void PingTest::DecodeICMPHeader(char *buf, int bytes,
struct sockaddr_in *from)
{//CTestDlg *dlg=(CTestDlg*)AfxGetMainWnd();
IpHeader *iphdr = NULL;
IcmpHeader *icmphdr = NULL;
unsigned short iphdrlen;
DWORD tick;
static int icmpcount = 0; iphdr = (IpHeader *)buf;
// Number of 32-bit words * 4 = bytes
iphdrlen = iphdr->h_len * 4;
tick = GetTickCount(); if ((iphdrlen == MAX_IP_HDR_SIZE) && (!icmpcount))
DecodeIPOptions(buf, bytes); if (bytes < iphdrlen + ICMP_MIN)
{
// AfxMessageBox("Too few bytes received");
return;
}
icmphdr = (IcmpHeader*)(buf + iphdrlen); if (icmphdr->i_type != ICMP_ECHOREPLY)
{
return;
}
if (icmphdr->i_id != (USHORT)GetCurrentProcessId())
{
return;
}
icmpcount++;
PingCount = icmpcount;
return;
}void CHubTestDlg::OnTest() //啟動兩個线程
{
ResetStatus();
UpdateData(TRUE);
m_Ping_Status = _T("Testing...");
UpdateData(FALSE);
m_PingStat_Control2.SetWindowText("Testing...");//////////////////create the thread:
PingThreadHandle1=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PingTthread1,this, 0, &PingThreadId1);
if (!PingThreadHandle1)
MessageBox ("MainWndProc(): Error creating read thread");
// Sleep(1000);
ResumeThread(PingThreadHandle1);
// Sleep(500);
PingThreadHandle2=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PingTthread2,this, 0, &PingThreadId2);
if (!PingThreadHandle2)
MessageBox ("MainWndProc(): Error creating read thread");
Sleep(1000);
ResumeThread(PingThreadHandle2);
Sleep(500);}
void CHubTestDlg::PingTthread1(LPVOID pParam)
{
CHubTestDlg *pDlg = (CHubTestDlg*)pParam;
PingTest Hub_Ping1;
DWORD Pingexitcode;
// The sub-function for PING test
Hub_Ping1.tping("10.132.227.1", PING_NUM);
if(Hub_Ping1.PingCount > 0) // Ping Test Succeed
{
pDlg->m_PingStatControl.SetWindowText("PASS");
iResultIp1=TRUE;
}
else
{
iResultIp1=FALSE;
pDlg->m_PingStatControl.SetWindowText("Failed");
// Continue = FALSE;
// WhichStepIsFailure = 2;
// strTemp = "Ping Test Failure.";
// AddLog(strTemp, LOGON);
}
}void CHubTestDlg::PingTthread2(LPVOID pParam)
{
CHubTestDlg *pDlg = (CHubTestDlg*)pParam;
PingTest Hub_Ping2; while(1)
{
SuspendThread(pDlg->PingThreadHandle2); // The sub-function for PING test
Hub_Ping2.tping(PING_IPADDR.GetBuffer(MAX_PATH), PING_NUM);
if(Hub_Ping2.PingCount > 0) // Ping Test Succeed
{
pDlg->m_PingStat_Control2.SetWindowText("2 PASS");
}
else
{
pDlg->m_PingStat_Control2.SetWindowText("2 Failed");
}
}
}為什么會出現相同的結果,其實應是不同的.
的值,在另一個线程中也調用PingTest功能,讀取PingCount,我知道這兩個pingcount的值是不同的,但這兩個PingCount的值為什么在兩個线程中讀取的时候都一樣呢?應該不同才對呀?
代碼如下:
class PingTest
{public:
int PingCount; /* number of ping operation successful*/
}
void PingTest::DecodeICMPHeader(char *buf, int bytes,
struct sockaddr_in *from)
{//CTestDlg *dlg=(CTestDlg*)AfxGetMainWnd();
IpHeader *iphdr = NULL;
IcmpHeader *icmphdr = NULL;
unsigned short iphdrlen;
DWORD tick;
static int icmpcount = 0; iphdr = (IpHeader *)buf;
// Number of 32-bit words * 4 = bytes
iphdrlen = iphdr->h_len * 4;
tick = GetTickCount(); if ((iphdrlen == MAX_IP_HDR_SIZE) && (!icmpcount))
DecodeIPOptions(buf, bytes); if (bytes < iphdrlen + ICMP_MIN)
{
// AfxMessageBox("Too few bytes received");
return;
}
icmphdr = (IcmpHeader*)(buf + iphdrlen); if (icmphdr->i_type != ICMP_ECHOREPLY)
{
return;
}
if (icmphdr->i_id != (USHORT)GetCurrentProcessId())
{
return;
}
icmpcount++;
PingCount = icmpcount;
return;
}void CHubTestDlg::OnTest() //啟動兩個线程
{
ResetStatus();
UpdateData(TRUE);
m_Ping_Status = _T("Testing...");
UpdateData(FALSE);
m_PingStat_Control2.SetWindowText("Testing...");//////////////////create the thread:
PingThreadHandle1=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PingTthread1,this, 0, &PingThreadId1);
if (!PingThreadHandle1)
MessageBox ("MainWndProc(): Error creating read thread");
// Sleep(1000);
ResumeThread(PingThreadHandle1);
// Sleep(500);
PingThreadHandle2=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PingTthread2,this, 0, &PingThreadId2);
if (!PingThreadHandle2)
MessageBox ("MainWndProc(): Error creating read thread");
Sleep(1000);
ResumeThread(PingThreadHandle2);
Sleep(500);}
void CHubTestDlg::PingTthread1(LPVOID pParam)
{
CHubTestDlg *pDlg = (CHubTestDlg*)pParam;
PingTest Hub_Ping1;
DWORD Pingexitcode;
// The sub-function for PING test
Hub_Ping1.tping("10.132.227.1", PING_NUM);
if(Hub_Ping1.PingCount > 0) // Ping Test Succeed
{
pDlg->m_PingStatControl.SetWindowText("PASS");
iResultIp1=TRUE;
}
else
{
iResultIp1=FALSE;
pDlg->m_PingStatControl.SetWindowText("Failed");
// Continue = FALSE;
// WhichStepIsFailure = 2;
// strTemp = "Ping Test Failure.";
// AddLog(strTemp, LOGON);
}
}void CHubTestDlg::PingTthread2(LPVOID pParam)
{
CHubTestDlg *pDlg = (CHubTestDlg*)pParam;
PingTest Hub_Ping2; while(1)
{
SuspendThread(pDlg->PingThreadHandle2); // The sub-function for PING test
Hub_Ping2.tping(PING_IPADDR.GetBuffer(MAX_PATH), PING_NUM);
if(Hub_Ping2.PingCount > 0) // Ping Test Succeed
{
pDlg->m_PingStat_Control2.SetWindowText("2 PASS");
}
else
{
pDlg->m_PingStat_Control2.SetWindowText("2 Failed");
}
}
}為什么會出現相同的結果,其實應是不同的.
為什么會一樣呢?