When is the parameter passed by reference updated?

Suppose I have a method like this:

public void MyCoolMethod(ref bool scannerEnabled) { try { CallDangerousMethod(); } catch (FormatException exp) { try { //Disable scanner before validation. scannerEnabled = false; if (exp.Message == "FormatException") { MessageBox.Show(exp.Message); } } finally { //Enable scanner after validation. scannerEnabled = true; } } 

And it is used as follows:

  MyCoolMethod(ref MyScannerEnabledVar); 

The scanner can be launched at any time in a separate thread. The idea is to prevent this if we handle the exception.

The question I have is, does the MyCoolMethod call cause the update of MyScannerEnabledVar when installing scannerEnabled or its updating after the method exits?

Note: I did not write this code, I am just trying to reorganize it safely.

+4
source share
4 answers

You can think of ref as creating an alias for a variable. This does not mean that the variable that you pass is "passed by reference", it is that the parameter and the argument are the same variable, just two names. Therefore, the update immediately updates the other, because there really aren't two things.

As Slacks notes, there are situations in VB that use copy-in-copy-out semantics. There are also, if I remember correctly, rare and obscure situations in which expression trees can be compiled into copy-in-copy code, but I don’t remember the details.

If this code is designed to update a variable for reading in another thread, then the fact that the variable is updated immediately is misleading. Remember that on multiple threads, you can observe that reading and writing move forward and backward in time with respect to each other if reads and writes are unstable. If the goal is to use this variable as a mechanism for exchanging across multiple threads, then they use an object specifically designed for this purpose, which is safe for this purpose. Use some kind of wait descriptor or mutex or something else.

+5
source

It is updated in real time as it is assigned inside the method.

When you pass a parameter by reference, the runtime passes (equivalent) a pointer to the field or variable to which you referred. When a method assigns to this parameter, it directly assigns the value to which the link refers.

Please note that this is not always true in VB.

+4
source

Yes, it will be set when the variable is set in the method. It might be better to return true or false if the scanner is turned on and not pass it as ref arg

+1
source

The situation requires more than a simple refactor. The code you published will depend on the race conditions. A simple solution is to block an unsafe method, thereby causing threads to jump in line. So it is, because of this code there may be some errors in the application, but it is impossible to say what exactly they are, without knowing much more about your requirements and implementation. I recommend that you proceed with caution, mutex / lock is a simple fix, but can greatly affect performance. If this bothers you, then you should all consider the best flow protected solution.

+1
source

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


All Articles