473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to make a Thread Safe Shared Function

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
2 9231
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Andy Chang | last post by:
Hi, If I have this function void DoSomething(int& index) { ::Sleep(10000); DoSomethingWith(index); } Is the variable index thread safe? Meaning that if I call this from two
9
by: rnn98 | last post by:
hi, my multithread application, running under solaris box, is crashing eventually. I tried to spot and substitute functions not "thread safe", but I guess my search wasn't good enough. I have put...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
4
by: | last post by:
I find in the documentation the following Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not...
6
by: iam1708 via DotNetMonster.com | last post by:
I was looking at the docs for Thread and can't understand the different between "unnamed data slot "and "data slot".and docs in Thread.GetData() say "Threads use a local store memory mechanism to...
7
by: Chad Zalkin | last post by:
We are evaluating some old code that was written as part of our math library. This code uses some optimizations that I'm not sure are necessary or safe, but is a source of debate between my...
12
by: David | last post by:
Below are three classes for a console application. If put into three separate files, the sub main() will launch multiple threads adding and removing the same value. At the end we expect the value...
5
by: Med | last post by:
Hi, Could someone please tell me if I undrestand the concept of "Thread Saftey" correctly in Multi-threaded web application (asp.net 2) using following examples: 1. Class A is NOT a thread...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.