thread-safe function
thread-safe function
thread-safe function
thread-safe function
thread-safe function
thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function
thread-safe function thread-safe function
thread-safe function
Go Back  Xtreme Visual Basic Talk > > > thread-safe function


Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2011, 12:38 AM
jaccog's Avatar
jaccog jaccog is offline
Regular
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 96
Question thread-safe function


Hi,

I need a function that will be called from many points in my code in a multi-thread environment.
Below is just a sample function, which converts a terminal command to a datapacket which is ready for transmission by adding controlcharacters and a length-check.

Code:
        Function MakePacket(ByVal Data As String) As String
        Dim ReturnValue As String = Chr(2) & Data
        If Not Data.EndsWith(ControlChars.Cr) Then Data &= ControlChars.Cr
        Data &= (Data.Length - 1).ToString
        ReturnValue &= Chr(3)
        Return ReturnValue
    End Function
How can I make this function available globally in my code without having it to include in a class which has to be instanciated for every call? My application uses several threads so it needs to be thread safe.
I am not sure which path to go, using STATIC, SHARED, etc.
I am hoping for something like you call a sort on an array like ARRAY.SORT(...)

Thanks!
__________________
Jacco.
-The one who says it cannot be done should never interrupt the one who is doing it-
Reply With Quote
  #2  
Old 01-03-2011, 04:48 AM
Goggy's Avatar
Goggy Goggy is online now
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default

I think "Public shared function" would do the trick for you.
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #3  
Old 01-03-2011, 06:35 AM
AtmaWeapon's Avatar
AtmaWeaponthread-safe function AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

"Shared" functions behave like static functions in other languages. VB picked its terminology to make this explanation fit:

When a member is not shared, every instance of every class gets its own copy of the member. For properties and fields, this means that object 1's Foo value is completely independent of object 2's. For methods this means something that breaks the imagery so we'll ignore it The point is non-shared members cannot be accessed without a reference to an object because you need to say which object the member belongs to.

When a member is shared it belongs to the class, not the individual objects. Object 1's shared Foo is the same property as object 2's shared Foo, and if you change the value both objects see the new value. Note this is *not* to say they each have the value and it's connected by magic: they literally both point to the same value in memory. Since shared members don't belong to a particular object, you use the class name to reference them. (I think VB .NET lets you use an object reference, but professionals prefer to use the class name so it's clear a shared member is being used.) By that I mean if SomeClass has a public Shared Sub DoSomething(), you would call it like this:
Code:
SomeClass.DoSomething()
Modules are a special VB .NET construct in which all members are Shared by default. C# expresses this concept by making you declare the class static, but there's a subtle difference between a static class and a Module: all of a module's members are imported into the global namespace. What does this mean? Assume you have a module Utilities with a method named CreateDatabase(). You could reference it using the class name:
Code:
Utilities.CreateDatabase()
But since it's a module and CreateDatabase() will be in the global namespace, you can call it from anywhere without using the module name:
Code:
CreateDatabase()
Personally I don't like this and I think it can (and usually does) lead to more confusing code. Prefer shared methods.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 01-03-2011, 06:50 AM
Roger-U Roger-U is offline
Centurion
 
Join Date: Apr 2008
Location: Surrey, UK
Posts: 111
Default

Quote:
(I think VB .NET lets you use an object reference,
VS2003 did, VS2008 gives an error "Access of shared member[...] through an instance; expression will not be evaluated"

but despite the error it does still work! probably for compatibility reasons. Though, of course, the correct way is by reference to the class name and not via the instance.

(Maybe you can change the error/warning via the Project Properties page?)

Last edited by Roger-U; 01-03-2011 at 07:53 AM.
Reply With Quote
  #5  
Old 01-04-2011, 02:13 AM
jaccog's Avatar
jaccog jaccog is offline
Regular
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 96
Default

Hi Atma,

Thanks for your answer. There is one thing unclear to me though;
If I'm using PUBLIC SHARED FUNCTION...
there will only one intance of the function excist in the code.
But how about the variables inside the function? When making calls from different threads simultanously, will that not interfere with the variale 'ReturnValue' I declared? I would say that all threads are working with the same variable and thus interfering with each other which makes ReturnValue an unpredictable result.
Am I right, or does this SHARED include some underwater syncklock machanism?

Thanks again!
__________________
Jacco.
-The one who says it cannot be done should never interrupt the one who is doing it-
Reply With Quote
  #6  
Old 01-04-2011, 02:23 AM
Roger-U Roger-U is offline
Centurion
 
Join Date: Apr 2008
Location: Surrey, UK
Posts: 111
Default

no need to worry! each time you call a function a new set of "variables inside the function" is created. If you do want a single variable for all the calls the declare it with Static
Reply With Quote
  #7  
Old 01-04-2011, 06:46 AM
AtmaWeapon's Avatar
AtmaWeaponthread-safe function AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Local variables are unique to each execution of the method, as Roger-U pointed out. There is a concept of a static local variable, and oddly enough the VB designers chose Static rather than Shared for the keyword:
Code:
Sub DoSomething()
    Static x As Integer = 0
    ....
End Sub
Since this is a variable that multiple threads can access simultaneously, you get all the synchronization worries of a field. I don't tend to like these variables because of that: I'm not as paranoid with these when analyzing threading issues.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 01-04-2011, 04:05 PM
jaccog's Avatar
jaccog jaccog is offline
Regular
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 96
Default

Thank you both Atma and Roger, for clearing things up!

Greetings from the Netherlands.
__________________
Jacco.
-The one who says it cannot be done should never interrupt the one who is doing it-
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
thread-safe function
thread-safe function
thread-safe function thread-safe function
thread-safe function
thread-safe function
thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function thread-safe function
thread-safe function
thread-safe function
 
thread-safe function
thread-safe function
 
-->