Right after I made the post I stumbled accross something that I could use for
thread safety, so I thought I would post in case someone else needs it for a
solution.
Solution:
void Foo()
{
// At the beginning of your function.
System::Threading::Monitor::Enter(this);
... Do some code work
// Add the end of your function (this has to be called)
System::Threading::Monitor::Exit(this);
}
> Greetings,
>
[quoted text clipped - 6 lines]
>
> Thanks in advance for any suggestions.
Carl Daniel [VC++ MVP] - 15 Jul 2005 16:37 GMT
> Right after I made the post I stumbled accross something that I could
> use for thread safety, so I thought I would post in case someone else
[quoted text clipped - 11 lines]
> System::Threading::Monitor::Exit(this);
> }
That's precisely what the C# 'lock' construct does.
-cd
Jochen Kalmbach [MVP] - 15 Jul 2005 19:16 GMT
Hi Carl!
>>Right after I made the post I stumbled accross something that I could
>>use for thread safety, so I thought I would post in case someone else
[quoted text clipped - 13 lines]
>
> That's precisely what the C# 'lock' construct does.
Almost... the C# also does an try-finally construct..
System::Threading::Monitor::Enter(this);
__try
{
// Do some code work
}
__finally
{
System::Threading::Monitor::Exit(this);
}

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nemanja Trifunovic - 15 Jul 2005 16:52 GMT
I think it would be pretty easy to wrap this into a RAII __nogc class
with a little help from gcroot.
BartMan - 15 Jul 2005 18:16 GMT
Hello Nemanja,
Thanks for the great idea, I didn't know about gcroot which is really
usefull. :)
Here is my attempt.
public __gc class lock
{
public:
lock(System::Object* obj)
{
threadObject = obj;
System::Threading::Monitor::Enter(threadObject);
}
~lock()
{
System::Threading::Monitor::Exit(threadObject);
}
protected:
gcroot<System::Object*> threadObject;
};
> I think it would be pretty easy to wrap this into a RAII __nogc class
> with a little help from gcroot.
Nemanja Trifunovic - 15 Jul 2005 18:30 GMT
I hope you mean __nogc lock.
__gc classes don't have deterministic destructors. Take a look at an
article I wrote a while ago on the topic:
http://www.codeproject.com/managedcpp/managedraii.asp
BartMan - 15 Jul 2005 18:55 GMT
Oops, you are right.
Very good article, thanks for the post!
> I hope you mean __nogc lock.
>
> __gc classes don't have deterministic destructors. Take a look at an
> article I wrote a while ago on the topic:
>
> http://www.codeproject.com/managedcpp/managedraii.asp