thread safe events

  • Thread starter Thread starter M. Raab
  • Start date Start date
M

M. Raab

i have a class that contains a method (not static) that is called on an
event by child threads of the class. i assume it is not threadsafe.
is there an attribute that i can set on the method that would cause the
runtime to handle the method in a thread safe way? or do i need to put in my
own locks?
thanks
m
 
M.

You can apply the MethodImpl attribute with a value of
MethodImplOptions.Synchronized on the method. However, this is bad for a
number of reasons. What I would recommend doing is using a lock section in
the method itself, locking on an object that is stored on the class level.

Hope this helps.
 
Hi,

Just as Nicholas said, MethodImplOptions.Synchronized is a choice for you.
Also you can apply the sychronization yourself. This mainly due to your
lock granularity.
The MethodImplOptions.Synchronized may only apply on a whole method, while
this may not be a need. For example, your method is big, and there are only
serveral sentences should be synchronized. At this time, you can apply
yourself mutex only to these sentences.
For self synchronization, there are a lot of ways in .Net Framework, such
as mutex class, event etc. You can find more information in
System.Threading namespace.

Btw: You can't use the [MethodImpl(MethodImplOptions.Synchronized)]
attribute on methods of a struct type (if you call the method you get a
runtime TypeLoadException). Something like this:

using System.Runtime.CompilerServices;
class Indirect
{
[MethodImpl(MethodImplOptions.Synchronized)] // compiles and runs ok
public void Method()
{
//...
}
}

struct Direct
{
[MethodImpl(MethodImplOptions.Synchronized)] // compiles ok, runtime
TypeLoadException
public void Method()
{
//...
}
}

Hope this helps, if you still have anything unclear, please feel free to
let me know
Have a nice day.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "M. Raab" <[email protected]>
| Subject: thread safe events
| Date: Wed, 22 Oct 2003 14:14:50 -0400
| Lines: 9
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 198-143-226-150.dsl.btitelecom.net 198.143.226.150
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193271
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| i have a class that contains a method (not static) that is called on an
| event by child threads of the class. i assume it is not threadsafe.
| is there an attribute that i can set on the method that would cause the
| runtime to handle the method in a thread safe way? or do i need to put in
my
| own locks?
| thanks
| m
|
|
|
 
Back
Top