473,803 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByVa l 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
"<Synchronizati on()> Public Class Class1 Inherits ContextBoundObj ect", 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 9269
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******** ******@TK2MSFTN GP11.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(ByVa l 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 "<Synchronizati on()> Public Class Class1 Inherits ContextBoundObj ect", 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****@microso ft.com> wrote in message
news:eW******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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(ByVa l 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
"<Synchronizati on()> Public Class Class1 Inherits ContextBoundObj ect",
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
5623
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
6023
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 localtime_r and asctime_r where I was using localtime and asctime. But I didn't find any correspondent _r function to "time".Also, I found that "readdir" is not thread safe. But what about "scandir"? Thanks for any help
11
2258
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 multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe since threads have their own
4
1315
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 guaranteed to be thread safe. Then, I go to the members documentation and I find public methods or
6
2713
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 store thread-specific data." what is "thread-specific data"? Best regards wmt email:iam1708@hotmail.com
7
2309
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 coworkers. Method 1 includes a temporary storage varriable at class scope. Method 2 includes a temporary storage varriable at method scope. Method 3 includes a temporary static storage varriable at method scope. Are any of the methods better than...
12
2048
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 for all Balances to be 0. When using an Integer things work fine. LONGS do not. We are using the Interlocked methods. I believe the Interlocked.Add method is not thread safe when using longs on 32bit systems. We are aware of...
5
1639
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 safe class: public sealed class A
15
2787
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 Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.' So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.