Rajagopal_JRaman

I have a function which add two numbers and return the answer. If the function is thread unsafe, what will happen .



Re: Visual Basic Language Threads

bmains

The two numbers are part of the method call, and the result is returned from the method I can't see how that would have an adverse affect since everything is local to the caller...



Re: Visual Basic Language Threads

SJWhiteley

I think the world will stop rotating and we will all fly off into space...

Okay, seriously, it depends. Where are these 'two numbers' As stated, if they are local to the method, then (probably) not much will happen. If they are global, then you may (will!) see problems. Can you give an example






Re: Visual Basic Language Threads

Rajagopal_JRaman

Hi bmains: Thanks for your answer.

Actually I want to know about the output of the thread unsafe codes. I have seen in MSDN where these x keyword/class/methods/attributes are thread unsafe. If we use them in the coding, what will happen What will they come to tell For example we have a set of code like this

Integer x , y, z

X = 1

Y = 2

Z = 0

Z = X + Y Let us assume this line is a thread unsafe operation and leave all the other things that global, local, arithmetic operations and etc. What will be the answer Z = any random number, Z = 0 (as assigned value). Finally my questions are 1. What is the meaning of thread unsafe 2. Where is it applicable


Kind regards

Rajagopal. J






Re: Visual Basic Language Threads

Rajagopal_JRaman

Hi whiteley : Thanks for your ans.

Actually I want to know about the output of the thread unsafe codes. I have seen in MSDN where these x keyword/class/methods/attributes are thread unsafe. If we use them in the coding, what will happen What will they come to tell For example we have a set of code like this

Integer x , y, z

X = 1

Y = 2

Z = 0

Z = X + Y Let us assume this line is a thread unsafe operation and leave all the other things that global, local, arithmetic operations and etc. What will be the answer Z = any random number, Z = 0 (as assigned value). Finally my questions are 1. What is the meaning of thread unsafe 2. Is it a probability 3. Where is it applicable 4. How should we avoid it


Kind Regards,
Rajagopal. J




Re: Visual Basic Language Threads

bmains

Hey,

Thread unsafe is often applicable when multiple objects are sharing the same property. For instance, the classic example of a bank account, where multiple transactions can hit against your bank account and credit/debit. The value would be stored in a bank account class, and it would have to use locking to ensure the value isn't modified at the same time.

In your example above, it depends how X and Y are set. Are they passed into the method and are available only locally Probably not a problem. Are they static properties in an object, and you need to set those first, then use a method to get the calculated result Definitely, though the code needs to be grouped together and locked in a different place. What I mean by that is, you need to lock the code that sets the two variables and calls the calculate method; you can't have locking code within the object that has the X/Y properties because you need to lock down the sequence of transactions.





Re: Visual Basic Language Threads

SJWhiteley

Thread safety has to do with how computers work, and the fact that we are dealing with a high level language.

Each line of code that we type may be (and is likely to be) more than one 'line' of code the processor understands - e.g. assembly language.

Using your example:

X = 1

Y = 2

Z = X + Y

is a multistep operation:

1: Get the value of X

2. Get the value of Y

3. Add the values together

4. Put the result into Z

Another part of the program may modify the values, or even use the values while the above operation is in progress. Lets say that X needs to be also incremented, but it is incremented on a different thread:

X = X + 1

The processor may execute this code while our above operation is in progres. So, what are the values of X, Y and Z after performing the Z=X+Y operation

it's possible that:

X=2

Y=2

Z=3

(obviously Z does NOT equal X+Y).

This could occur if the value of X is incemented after step 1 and prior to our use of variable Z. This is a fairly contrived example, but it exemplifies the problem. This can lead to huge errors in your program, including crashes.

This applies whenever you have more than one thread. Most methods are not thread safe (the overhead of making a method thread safe can be huge). What thius means that the results of the operation are unpredictable (as in the above example) if that method is called from multiple threads.

Indeed, even if a method is not called from multiple threads, but other methods are, does not mean that the operation is 'safe' to call in a multithreaded application; the above example demonstrates this: Z=X+Y is not thread safe, even if it is called (performed) on one thread.

There are several ways to overcome this: the first is to design the application such that interactions such as the above cannot occur - this is by far the most important technique! Even a single second thread can mean a lot of thought required to design it correctly. The second avenue of attack is to syncronize the threads - there are many techniques to do so: design it for the most simplest technique available. Troubleshooting multithreaded applications can be extremely frustrating and time consuming, and it is best to really limit the thread interaction as much as possible.

Threading: http://msdn2.microsoft.com/en-us/library/3e8s7xdd(vs.80).aspx






Re: Visual Basic Language Threads

Rajagopal_JRaman

Hi, Thanks

Please verify what I understood from your answers

  1. Thread unsafe is often applicable when multiple objects are sharing the same property.
  2. In the high level language, we should take care of thread safe/unsafe when multiple objects are sharing the same property.
  3. Thread unsafe is a certain probability (this is depending on the processor and OS) to miss/skip the ordered thread sequences.

If what I have understood from your answers is correct, do you have any idea about the term ¡°certain probability¡± (Numerical Values)

Kind Regards

Rajagopal J





Re: Visual Basic Language Threads

Rajagopal_JRaman

Hi Thanks

Please verify what I understood from your answers

  1. Thread unsafe is often applicable when multiple objects are sharing the same property.
  2. It is probability but very less chance to miss a thread.
  3. In the high level language, we should take care of thread safe/unsafe when multiple objects are sharing the same property.
  4. Thread unsafe is a certain probability (this is depending on the processor and OS) to miss/skip the ordered thread sequences.

If what I have understood from your answers is correct, do you have any idea about the term ¡°certain probability¡± (Numerical Values)

Kind Regards

Rajagopal J





Re: Visual Basic Language Threads

SJWhiteley

Certain probability: it will happen.

Because of the nature of the program, it's impossible to give an exact number. Because of the speed of computers, then a one in a million chance can be a high probability (perhaps could happen every minute). Again, it depends.

Even so, even an extremely low probability is unnacceptable. How would you feel if the description for the addiion operator was thus: usually, the addition operator will add the two numbers together, but there's a one in 10 billion chance that it'll not work. I certainly don't want that happening with my bank account....






Re: Visual Basic Language Threads

Rajagopal_JRaman

Hi whitely

Thanks for your answer. Now I have a clear idea about thread safe/unsafe, and we should assume thread unsafe(for all cases) is impossible. As you thought, if it will happen, the world will stop rotating and we will all fly off into space... Keep your answering style, it is beautiful.

Kind Regards

Rajagopal. J