I
Ioannis Vranos
In .NET multithreading we have to assign a thread to a method of a
separate object each time (and not two methods of the same object)?
In other words, why does this hung?
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
class SomeException
{};
__gc class SomeClass
{
int index;
//...
public:
// ...
void DoSomething()
{
Monitor::Enter(this);
throw SomeException();
// Modify index
Monitor::Exit(this);
}
void DoSomethingElse()
{
Monitor::Enter(this);
// Modify index
Monitor::Exit(this);
}
// ...
};
int main() try
{
SomeClass *ps= __gc new SomeClass;
Thread *pthread1= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass:oSomething) );
Thread *pthread2= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass:oSomethingElse) );
//Start execution of ps->DoSomething()
pthread1->Start();
//Start execution of ps->DoSomethingElse()
pthread2->Start();
}
catch(SomeException)
{
Console::WriteLine("SomeException caught in main thread!\n");
}
separate object each time (and not two methods of the same object)?
In other words, why does this hung?
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
class SomeException
{};
__gc class SomeClass
{
int index;
//...
public:
// ...
void DoSomething()
{
Monitor::Enter(this);
throw SomeException();
// Modify index
Monitor::Exit(this);
}
void DoSomethingElse()
{
Monitor::Enter(this);
// Modify index
Monitor::Exit(this);
}
// ...
};
int main() try
{
SomeClass *ps= __gc new SomeClass;
Thread *pthread1= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass:oSomething) );
Thread *pthread2= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass:oSomethingElse) );
//Start execution of ps->DoSomething()
pthread1->Start();
//Start execution of ps->DoSomethingElse()
pthread2->Start();
}
catch(SomeException)
{
Console::WriteLine("SomeException caught in main thread!\n");
}