VB.NET Shared Function if you call several times at once

I have a general function: -

Public Shared Function CalculateAreaFromRadius(ByVal radius As Double) As Double ' square the radius... Dim radiusSquared As Double radiusSquared = radius * radius ' multiply it by pi... Dim result As Double result = radiusSquared * Math.PI 'Wait a bit, for the sake of testing and 'simulate another call will be made b4 earlier one ended or such for i as Integer = 0 to integer.Max Next ' return the result... Return result End Function 

My questions:

  • If I have two or more threads in one vb.net application, and each of them simultaneously calls a shared function with different RADIUS, will each of them get its own AREA?

  • I want to know for each function call if it uses the same local variables or does each call create new instances of local variables?

  • Will the answers to the above questions be the same. If I have several (2+) single-threaded applications and all of them simultaneously call a function with a different RADIUS value?

I would appreciate your reply. Thanks.

+4
source share
2 answers

1) If I have two or more threads in the same vb.net application, and each of them calls a shared function simultaneously with different RADIUS, will each of them get its own AREA?

Yes, since the radius value is passed by value, and the method uses nothing but a local variable declaration.

2) I want to know for each function call if it uses the same local variables or each call creates new instances of local variables?

Each call creates a new instance of its local variables.

3) Will the answers to the above questions be the same. If I have several (2+) single-threaded applications and all of them simultaneously call a function with a different RADIUS value?

Yes. Again, since there is no common repository of information, and since all inputs are passed by value, it is thread safe.

+6
source

The function uses no external state . It only gets access to its local variables, so it’s completely safe to call it from different threads.

  • Yes
  • Local variables are specific to a particular call regardless of the thread the function is running on (think of a recursive function, each time you call a function, it will have a separate set of local variables).
  • Yes
+3
source

Source: https://habr.com/ru/post/1305151/


All Articles