By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,756 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,756 IT Pros & Developers. It's quick & easy.

How to make a Thread Safe Shared Function

P: n/a
I have been looking all over and have seen many possible ways to create a
Syncronized Shared Function in VB.NET, but I would like some advice that
will make my life easier. Given the following code how do I ensure that the
two methods are thread safe?

Public Class Class1
Public Shared Function addMe(ByVal i1 As Integer, ByVal i2 As Integer)
As Integer
Return i1 + i2
End Function

Public Shared Function makeString(ByVal s1 As String, ByVal s2 As
String) As String
Return s1.ToLower + s2.ToUpper
End Function
End Class

I know that I could put SyncLock in each of the methods or that I could use
"<Synchronization()> Public Class Class1 Inherits ContextBoundObject", but I
have a lot of code that needs updated and was also thinking that SyncLock
and that Syncronizing the entire class is a memory issue. Essentially what
I want to do is create a Public Shared Syncronized method.

Thanks,

-Aaron
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
As far as I can tell, both your methods are thread safe. Remember that each
thread that accesses these methods pushes the parameters onto its own
individual stack which means each has its own copy.

Read the last message by Stephen Martin about thread safety of methods in
general:
http://www.dotnet247.com/247referenc...29/149376.aspx

hopefully that should clear out any doubts you have regarding your methods.

hope that helps..
Imran.

"Aaron Cutlip" <ac*************@cox.net> wrote in message
news:O6**************@TK2MSFTNGP11.phx.gbl...
I have been looking all over and have seen many possible ways to create a
Syncronized Shared Function in VB.NET, but I would like some advice that
will make my life easier. Given the following code how do I ensure that the two methods are thread safe?

Public Class Class1
Public Shared Function addMe(ByVal i1 As Integer, ByVal i2 As Integer)
As Integer
Return i1 + i2
End Function

Public Shared Function makeString(ByVal s1 As String, ByVal s2 As
String) As String
Return s1.ToLower + s2.ToUpper
End Function
End Class

I know that I could put SyncLock in each of the methods or that I could use "<Synchronization()> Public Class Class1 Inherits ContextBoundObject", but I have a lot of code that needs updated and was also thinking that SyncLock
and that Syncronizing the entire class is a memory issue. Essentially what I want to do is create a Public Shared Syncronized method.

Thanks,

-Aaron

Nov 21 '05 #2

P: n/a
Thanks for the article, it really helps clear it up for me. I was making
the INCORRECT assumption that if two threads called the same shared function
at the same time that the parameters to that function would also be shared
and that the last one in would when therefore making the method
thread-UNsafe.

Thanks again.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
As far as I can tell, both your methods are thread safe. Remember that
each
thread that accesses these methods pushes the parameters onto its own
individual stack which means each has its own copy.

Read the last message by Stephen Martin about thread safety of methods in
general:
http://www.dotnet247.com/247referenc...29/149376.aspx

hopefully that should clear out any doubts you have regarding your
methods.

hope that helps..
Imran.

"Aaron Cutlip" <ac*************@cox.net> wrote in message
news:O6**************@TK2MSFTNGP11.phx.gbl...
I have been looking all over and have seen many possible ways to create a
Syncronized Shared Function in VB.NET, but I would like some advice that
will make my life easier. Given the following code how do I ensure that

the
two methods are thread safe?

Public Class Class1
Public Shared Function addMe(ByVal i1 As Integer, ByVal i2 As
Integer)
As Integer
Return i1 + i2
End Function

Public Shared Function makeString(ByVal s1 As String, ByVal s2 As
String) As String
Return s1.ToLower + s2.ToUpper
End Function
End Class

I know that I could put SyncLock in each of the methods or that I could

use
"<Synchronization()> Public Class Class1 Inherits ContextBoundObject",
but

I
have a lot of code that needs updated and was also thinking that SyncLock
and that Syncronizing the entire class is a memory issue. Essentially

what
I want to do is create a Public Shared Syncronized method.

Thanks,

-Aaron


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.