Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

How to make a function thread safe in mc++?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BartMan - 15 Jul 2005 16:20 GMT
Greetings,

I was looking at the C#'s "lock()" statement, and I was wondering if there
was an equivalent or recommended way of doing this in MC++.

I have a thread which I am calling a function from mc++, and I was wondering
what
the recommended ways of making it thread safe within the mc++ world.

Thanks in advance for any suggestions.
BartMan - 15 Jul 2005 16:31 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 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.