我有一个具有两个功能的线程类:
using namespace System::Threading;
public ref class SecThr
{
public:
int j;
void wralot()
{
for (int i=0; i<=400000;i++)
{
j=i;
if ((i%10000)==0)
Console::WriteLine(j);
}
}
void setalot()
{
Monitor::Enter(this);
for (int i=0; i<=400000;i++)
{
j=7;
}
Monitor::Exit(this);
}
};
int main(array<System::String ^> ^args)
{
Console::WriteLine("Hello!");
SecThr^ thrcl=gcnew SecThr;
Thread^ o1=gcnew Thread(gcnew ThreadStart(thrcl, &SecThr::wralot));
Thread^ o2=gcnew Thread(gcnew ThreadStart(thrcl, &SecThr::setalot));
o2->Start();
o1->Start();
o1->Join();
o2->Join();
所以,为了锁定“setalot”功能,我使用了 MOnitor::Enter-Exit 块。但是输出就像我只运行一个带有函数“wralot”的线程
0 10000 20000 30000 40000 7 //这里是“setalot”函数 60000 e tc
为什么“setalot”函数不会将所有输出数据更改为 const (7)