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 / C# / December 2005

Tip: Looking for answers? Try searching our database.

synchronous call

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ivan - 13 Dec 2005 20:05 GMT
how to make a single method thread safe?

example:
class A
{
  void func() {}
}

thread1:
A a1 = new A();
a.func();

thread2:
A a2 = new A();
a.func();

Ivan
Nicholas Paldino [.NET/C# MVP] - 13 Dec 2005 21:51 GMT
Ivan,

   The code that you just posted is actually thread safe because it doesn't
do anything.

   Making an object or method call thread-safe depends on the granularity
you are looking to achieve, along with the resources you are trying to make
thread safe.

   Can you be more specific about what you are trying to make thread-safe?

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> how to make a single method thread safe?
>
[quoted text clipped - 13 lines]
>
> Ivan
Ivan - 13 Dec 2005 21:59 GMT
A.func() needs to be application-wide thread safe.
Having the example in mind, if a1 enters func(), a2 should wait.

example:
 class A
 {
   void func() {}
 }

 thread1:
 A a1 = new A();
 a1.func();

 thread2:
 A a2 = new A();
 a2.func();

 Ivan
Nicholas Paldino [.NET/C# MVP] - 13 Dec 2005 23:54 GMT
Ivan,

   Check out the MethodImpl attribute, and pass in the
MethodImplOptions.Synchronized value to the attribute.

   However, this will make any call on the object (you need to apply it to
every method) hold whenever any other method is called on any other thread.

   Again, you didn't show anything that you were trying to synchronize, so
this is the most generic code you could use.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> A.func() needs to be application-wide thread safe.
> Having the example in mind, if a1 enters func(), a2 should wait.
[quoted text clipped - 14 lines]
>
>  Ivan
Ivan - 14 Dec 2005 21:54 GMT
>     Again, you didn't show anything that you were trying to synchronize, so ...
synchronized code is substituted for simplicity by A.func()
this is code that only one thread needs to be in.
if I make it static and use this attribute (thanks for the attribute), my problem's fixed.

example:

using System.Runtime.CompilerServices;

class A
{
  [MethodImplAttribute(MethodImplOptions.Synchronized)]
  public static void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();

Ivan

> Ivan,
>
[quoted text clipped - 3 lines]
>     However, this will make any call on the object (you need to apply it to
> every method) hold whenever any other method is called on any other thread.

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.