c++ 局部变量A为什么不能给局部变量B接收 求修改
#include<Windows.h>#include<iostream>DWORDWINAPIThread(LPVOIDlpParmeter);HANDLEg_hMut...
#include <Windows.h>
#include <iostream>
DWORD WINAPI Thread(LPVOID lpParmeter);
HANDLE g_hMutex;
int main()
{
static int u = 3 , i = 0;
static int b = 5 , a = 0;//在这里定义了变量
HANDLE* hThread = new HANDLE[u];
for(;i<u; i++)
{
g_hMutex=CreateMutex(NULL,FALSE,"MutexToRecive");
hThread[i]=CreateThread(NULL,0,Thread,&i,0,NULL);
Sleep(50);
}
SetEvent(g_hMutex);
WaitForMultipleObjects(i, hThread, TRUE, INFINITE);
for (; i<u; i++)
CloseHandle(hThread[i]);
return 0;
}
DWORD WINAPI Thread(LPVOID lpParmeter)
{ int idx = *(int*)lpParmeter;
for(;a<b;a++)//接收不到
{
WaitForSingleObject(g_hMutex, INFINITE);
if (a < b)//这里也是
{
Sleep(25);
printf("线程%d %d\n",idx,a);
ReleaseMutex(g_hMutex);
}
else
{
ReleaseMutex(g_hMutex);
break;
}
}
return 0;
} 展开
#include <iostream>
DWORD WINAPI Thread(LPVOID lpParmeter);
HANDLE g_hMutex;
int main()
{
static int u = 3 , i = 0;
static int b = 5 , a = 0;//在这里定义了变量
HANDLE* hThread = new HANDLE[u];
for(;i<u; i++)
{
g_hMutex=CreateMutex(NULL,FALSE,"MutexToRecive");
hThread[i]=CreateThread(NULL,0,Thread,&i,0,NULL);
Sleep(50);
}
SetEvent(g_hMutex);
WaitForMultipleObjects(i, hThread, TRUE, INFINITE);
for (; i<u; i++)
CloseHandle(hThread[i]);
return 0;
}
DWORD WINAPI Thread(LPVOID lpParmeter)
{ int idx = *(int*)lpParmeter;
for(;a<b;a++)//接收不到
{
WaitForSingleObject(g_hMutex, INFINITE);
if (a < b)//这里也是
{
Sleep(25);
printf("线程%d %d\n",idx,a);
ReleaseMutex(g_hMutex);
}
else
{
ReleaseMutex(g_hMutex);
break;
}
}
return 0;
} 展开
4个回答
2012-09-07
展开全部
static int b = 5 , a = 0;//在这里定义了变量
菜鸟认为是静态变量的问题
菜鸟认为是静态变量的问题
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c++最基础的东西都没学好,还搞多线程~~牛人~~
追问
= = 有什么办法? 别跟我说改为全局变量这个我也知道
如果你牛人 那么你请告诉我怎么办 菜鸟请教了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询