主题:c++创建多线程传参数出现问题
这是一个代理程序
void* hThread = CreateThread(NULL, 0, TCPProxyThread,se, 0, &idthread);
if (hThread == NULL)
{
cout<<"CreateThread() Error!"<<endl;
return;
}
为什么调用TCPProxyThread时参数se的内容就改变了,原先地址中有有效数据,而传过去之后就全部清零了?哪位大侠帮忙看看,有源程序,谢谢!
DWORD WINAPI TCPProxyThread(LPVOID param)
{
ServiceStruct* psvc = (ServiceStruct*)param;
DWORD id1,id2;
void* hThread1 = CreateThread(NULL, 0, TCPClientToServer,psvc, 0, &id1);
if (hThread1 == NULL)
{
cout<<"CreateThread() Error!"<<endl;
return 1;
}
void* hThread2 = CreateThread(NULL, 0, TCPServerToClient, psvc, 0, &id2);
if (hThread2 == NULL)
{
cout<<"CreateThread() Error!"<<endl;
return 1;
}
SuspendThread(GetCurrentThread());
return 0;
}
DWORD WINAPI TCPServerToClient(LPVOID param) //server to client
{
ServiceStruct* psvc = (ServiceStruct*)param;
char buf[1024];
int count;
while (true)
{
count = recv(psvc->svctcp_server,buf,1024,0);
SendBuffer(psvc->tcpsock,buf,count);
int i;
cin>>i;
cout<<"TCPServerToClient:server "<<psvc->svctcp_server<<endl;
cout<<"TCPServerToClient:client "<<psvc->tcpsock<<endl;
}
}
DWORD WINAPI TCPClientToServer(LPVOID param) //client to server
{
ServiceStruct* psvc = (ServiceStruct*)param;
char buf[1024];
int count;
while (true)
{
count = recv(psvc->tcpsock,buf,1024,0);
SendBuffer(psvc->svctcp_server,buf,count);
int i;
cin>>i;
cout<<"TCPClientToServer:server "<<psvc->svctcp_server<<endl;
cout<<"TCPClientToServer:client "<<psvc->tcpsock<<endl;
}
}
void* hThread = CreateThread(NULL, 0, TCPProxyThread,se, 0, &idthread);
if (hThread == NULL)
{
cout<<"CreateThread() Error!"<<endl;
return;
}
为什么调用TCPProxyThread时参数se的内容就改变了,原先地址中有有效数据,而传过去之后就全部清零了?哪位大侠帮忙看看,有源程序,谢谢!
DWORD WINAPI TCPProxyThread(LPVOID param)
{
ServiceStruct* psvc = (ServiceStruct*)param;
DWORD id1,id2;
void* hThread1 = CreateThread(NULL, 0, TCPClientToServer,psvc, 0, &id1);
if (hThread1 == NULL)
{
cout<<"CreateThread() Error!"<<endl;
return 1;
}
void* hThread2 = CreateThread(NULL, 0, TCPServerToClient, psvc, 0, &id2);
if (hThread2 == NULL)
{
cout<<"CreateThread() Error!"<<endl;
return 1;
}
SuspendThread(GetCurrentThread());
return 0;
}
DWORD WINAPI TCPServerToClient(LPVOID param) //server to client
{
ServiceStruct* psvc = (ServiceStruct*)param;
char buf[1024];
int count;
while (true)
{
count = recv(psvc->svctcp_server,buf,1024,0);
SendBuffer(psvc->tcpsock,buf,count);
int i;
cin>>i;
cout<<"TCPServerToClient:server "<<psvc->svctcp_server<<endl;
cout<<"TCPServerToClient:client "<<psvc->tcpsock<<endl;
}
}
DWORD WINAPI TCPClientToServer(LPVOID param) //client to server
{
ServiceStruct* psvc = (ServiceStruct*)param;
char buf[1024];
int count;
while (true)
{
count = recv(psvc->tcpsock,buf,1024,0);
SendBuffer(psvc->svctcp_server,buf,count);
int i;
cin>>i;
cout<<"TCPClientToServer:server "<<psvc->svctcp_server<<endl;
cout<<"TCPClientToServer:client "<<psvc->tcpsock<<endl;
}
}