|
-
Sep 4th, 2010, 08:45 AM
#1
Thread Starter
New Member
shared sub and multiple threads
Hi,
I have a created a class, which contains a sub routine.
This sub routine, should ideally be accessable by objects which are not members of the class, so I would like to make the sub, a 'shared sub' (call it Sub1).
Question: I am using many threads and there will be possibly 100s of 1000s of threads all wanting to use Sub1 (which is at present, is not shared).
At present Sub1 , is not a shared sub, so every time a thread wishes to use Sub1 , I am creating a (dummy) instance of the class and then applying Sub1, to that instance.
My program works flawlessly using the above technique.
Question: If I were to turn Sub1 (which is not currently shared) into a shared sub, and directly had various threads calling Sub1, would there be any issues with locking, ie. would it possible for 2 threads to enter that thread and start overwriting variables, which another thread has written to?
NOTE: there are no shared variables/properties in Sub1 and all variables used in Sub1 are declared within the sub itself.
Thanks
-
Sep 4th, 2010, 09:00 AM
#2
Re: shared sub and multiple threads
Shared has little to do with this, except that a shared sub would not require an instance of the class. Whether that would cause any issues with the threads takes a little further thought. If the current sub alters any of the class variables, then those variables would have to be shared, as well, or the sub would cease to work when it was shared. If the sub does not alter any class variables, then, not only can it be shared more easily, but it might work just fine with the threads.
Whenever a sub is called, regardless of the thread, it will create local variables. If your sub is working only with local variables, then you should have no problem with the threading, whether the sub is shared or not. If the sub is altering variables outside of the sub, such as class level variables, or global variables, then you would have potential problems with the sub. However, if the sub was altering variables outside of the class, then you have those problems already. Since you say the program is working flawlessly, then I assume that either the sub doesn't alter variables outside of the class, or you are handling those concurrency issues correctly already (or you just haven't encountered a problem yet).
So, it comes down to whether the sub is altering class level variables as to whether you can even make it shared and whether or not you will need to worry about concurrency issues when you make that change.
My usual boring signature: Nothing
 
-
Sep 4th, 2010, 09:36 AM
#3
Thread Starter
New Member
Re: shared sub and multiple threads
Hi.
Thanks for the reply.
The sub is not altering any shared variables.
All variables which are being worked upon within the sub are declared locally within the sub.function itself.
What I am worried about is the following (shown using an example, fake class/functioin):
class exampleClass
public shared Function IsEmpty(byval text as string)
if string.isnullorempty(text) then........line1
return true
else
return false
end if
end function
end class
Now, lets say I have 2 threads. They are both going to use the IsEmpty func.
Both threads call the func (almost) at the same time.
Thread1 calls the func: IsEmpty("some text")
"some text" is entered into IsEmpty
Thread1 then moves onto line1, but doesnt execute it yet.
Meanwhile,
Thread2 calls the func: IsEmpty("")
Thread2 enters the IsEmpty func and text = "".
Now Thread1 processes line1
"if string.isnullorempty(text) then". although Thread1 had text = "some text", the Thread2 has called the func and has changed 'text' to "".
So now, Thread1 will be working on text = "", rather than "some text".
In the above way, is it possible for 2 threads to enter the same sub/function and change the local variables?
Last edited by sunama; Sep 4th, 2010 at 10:00 AM.
-
Sep 4th, 2010, 09:54 AM
#4
Re: shared sub and multiple threads
They aren't actually in the same subroutine, and all local variables are isolated to the thread that they were created on. So that would suggest that you are completely safe....but not quite. If the variable being passed in is a reference type, and the sub changes the variable, then you have a problem. If the sub never changes the variables passed to it, or if the variables passed in are all value types passed ByVal, then there will be no problems.
If you are passing in a reference type, and the sub changes that reference type (which might include actually changing text, rather than just checking to see whether it is empty), then you have to make sure that each thread that calls the sub passes in a copy of the reference type.
In your example, there will be no issues, because the sub doesn't change the argument, it just reads it. In that case, each thread will operate independently without any issues.
My usual boring signature: Nothing
 
-
Sep 4th, 2010, 10:03 AM
#5
Thread Starter
New Member
Re: shared sub and multiple threads
Okay...so byval is fine. I understand this.
Just to clarify, when you have 2 threads using the same sub/function (as in the example above), are you saying that each thread will create and operate in a separate instance of the sub/function and that 2 threads will never enter the same sub/function?
-
Sep 4th, 2010, 10:18 AM
#6
Re: shared sub and multiple threads
ByVal is NOT safe on its own, unless the argument is a value type. Oddly, that discussion is going on in another very current thread:
http://www.vbforums.com/showthread.php?t=626732
What is safe is if the sub doesn't make any changes to the arguments, or if the arguments are value types passed ByVal. If the sub makes changes to the arguments, and the arguments are reference types, then you are not safe, regardless of whether they are passed ByVal or not, for the reasons noted in the thread I linked to.
The two threads will execute the same code. They will cover the same instructions. However, each one will keep its own set of local variables. When thread A is given time on the processor, it loads all of its variables into the processor registers, and performs operations until it is time to give up the processor to another thread, at which time it takes all of the data it is working on, and stores it back to its own special memory area. So two threads can work with the same sequence of instructions, but each will be using only its own data. The only exception to this is the issue with reference types being passed in as arguments, because in this case there is only one object, and each thread just has a different copy of the address of that one object.
My usual boring signature: Nothing
 
-
Sep 4th, 2010, 10:42 AM
#7
Thread Starter
New Member
Re: shared sub and multiple threads
Mr Shaggy Hiker.
Thank you sir.
The following was the key point:
 Originally Posted by Shaggy Hiker
The two threads will execute the same code. They will cover the same instructions. However, each one will keep its own set of local variables. When thread A is given time on the processor, it loads all of its variables into the processor registers, and performs operations until it is time to give up the processor to another thread, at which time it takes all of the data it is working on, and stores it back to its own special memory area. So two threads can work with the same sequence of instructions, but each will be using only its own data.
-
Sep 4th, 2010, 11:16 AM
#8
Re: shared sub and multiple threads
I would say that this is more of the key point:
What is safe is if the sub doesn't make any changes to the arguments, or if the arguments are value types passed ByVal. If the sub makes changes to the arguments, and the arguments are reference types, then you are not safe, regardless of whether they are passed ByVal or not, for the reasons noted in the thread I linked to
As long as you clearly understand that, as well as the point you highlighted, then you will be fine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|