CDisplay的CPP中定义了一个全局变量strURL,
CDisplay::strDonw(strDownload)
{
strURL=strDownload;
//调用一个线程,此线程中使用strURL这个全局变量
AfxBeginThread(...);
}Void main()
{
CDisplay A;
CDisplay B;
A.strDownload("192.168.1.15");
B.strDownload("192.168.1.40");
}
为什么程序调试过程发现,最后两个对象的线程中strURL都是192.168.1.40,请问怎么解释?全局变量好像作用空间为一个对象吧!
CDisplay::strDonw(strDownload)
{
strURL=strDownload;
//调用一个线程,此线程中使用strURL这个全局变量
AfxBeginThread(...);
}Void main()
{
CDisplay A;
CDisplay B;
A.strDownload("192.168.1.15");
B.strDownload("192.168.1.40");
}
为什么程序调试过程发现,最后两个对象的线程中strURL都是192.168.1.40,请问怎么解释?全局变量好像作用空间为一个对象吧!
func()
{
a=3;a=4;
}
你觉得最后a==?如果你想不同的线程对应不同的ip
就把ip的指针作为参数赋给线程函数
AfxBeginThread(ThreadProc,&strURL);
{
strURL=strDownload;
//调用一个线程,此线程中使用strURL这个全局变量
AfxBeginThread(ThreadProc,&strDownload);
}
//每个线程都从参数中能得到不同的strDownload。
正如madhappy(545454)所说的。
{
strURL=strDownload;
////////////???????????????????????
AfxBeginThread(...);
}Void main()
{
CDisplay A;
CDisplay B;
A.strDownload("192.168.1.15");
B.strDownload("192.168.1.40");
}
个人觉得是个时间问题,因为是多线程,所以是并发的,当A的程序执行到//????????位置时,B
的strDownload()又开始了,在A的线程还为开启前,B的strURL=strDownload;已经改变了全局变量.
A.strDownload("192.168.1.15");
B.strDownload("192.168.1.40");
第2次调用B.strDownload("192.168.1.40");就把strURL修改为192.168.1.40,我刚才的回复是错误的,不好意思哦