Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Multithreading in VB.NET  »  A Quick Word on the volatile Keyword
Multithreading in VB.NET
by John Spano | Published  07/16/2005 | .NET Framework | Rating:
A Quick Word on the volatile Keyword

A Quick Word on the volatile Keyword

 

    In your reading or study of .Net code, the volatile C# keyword might come up.  This keyword does not exist in Visual Basic.  Don’t worry though; it doesn’t add any functionality to C# that can’t be done with the other synchronization objects discussed in this case study.

    The volatile keyword tells the compiler that the variable it references could change at anytime and that no optimizations should be done to it.  It will prohibit the compiler from storing the variable in a register and force it read it new from memory each time.

    Variables marked as volatile aren’t necessarily thread safe.  They only insure that each read of the variable is the latest information.  To see what a declaration looks like look at the following code snip-it, which declares an Integer variable as volatile.

private volatile int MyInteger;

    Use of Monitor is a much safer and better way to handle synchronization.  It guarantees that the variable is up to date as only one thread is accessing the variable at a time.  It is safe to replace volatile variable access with Monitor blocks of code or any other synchronization method discussed in the case study that fit your needs.  Good synchronization practice will eliminate the need for volatile.

Comments    Submit Comment

Comment #1  (Posted by Steven Leadbeater on 08/02/2005)
Rating
Oh My God. this is absolutely brilliant, clear concise and everything works with a minimal amount of tweaking by FAR the best post on threading i have read including anything on MSDN. only wish i could give it a higher rating, nice1
 
Comment #2  (Posted by Ran Oren on 08/14/2005)
Rating
Thanks a lot!!! So clear and simple!
 
Comment #3  (Posted by an unknown user on 08/24/2005)
Rating
Gave me exactly the information I was looking for in a clear way. Thanks
 
Comment #4  (Posted by an unknown user on 08/24/2005)
Rating
None of the books or articles i've read throughout my career has made me understand multi-threading, more than this article.
 
Comment #5  (Posted by an unknown user on 08/24/2005)
Rating
None of the books or articles i've read throughout my career has made me understand multi-threading, more than this article.
 
Comment #6  (Posted by Sulakshana on 08/25/2005)
Rating
Too good,wish I had found this earlier.
 
Comment #7  (Posted by Anonymous on 09/03/2005)
Rating
I agree wholeheartedly with the other comments. This is the only lucid, accessible article I've ever read on the subject. Mr. Spano ought to consider writing a book on the subject--he obviously knows his stuff and how to explain it for us mere mortals.
 
Comment #8  (Posted by an unknown user on 09/07/2005)
Rating
It covered all the salient points.
 
Comment #9  (Posted by an unknown user on 09/21/2005)
Rating
only one word: Excellent!
 
Comment #10  (Posted by Lucas on 10/06/2005)
Rating
Excellent, perfect, great, but the code snippets could be formatted better.
 
Comment #11  (Posted by an unknown user on 12/01/2005)
Rating
good wana more like this
 
Comment #12  (Posted by Sandy Righolme on 12/20/2005)
Rating
Excellent article. Thanks.
 
Comment #13  (Posted by GaZ on 03/30/2006)
Rating
Absolutely spot on article. Very very helpful thanks!
 
Comment #14  (Posted by Marko Hocevar on 04/03/2006)
Rating
I found the article and was very excited to read it...
I also used a piece of code from here in one of my apps but recently discovered that is not working properly. Because threads never share local dynamic variables example in second Public Sub Foo() where piece of code is locked with local variable (=new object()) is not working at all!!

best regards
Mare
 
Comment #15  (Posted by Zoila on 05/16/2006)
Rating
Great Article, so clear and concise. A monkey can understand it... well maybe not a monkey, but I understood it.

Thanks ...
 
Comment #16  (Posted by an unknown user on 06/25/2006)
Rating
Very well written article.
 
Comment #17  (Posted by matroskin on 07/27/2006)
Rating
The example with invoke method, about synchronization of windows forms, does not work, exception in run time
 
Comment #18  (Posted by rimanning on 08/08/2006)
Rating
The other reviewers got it right - this article explains multithreading with great clarity. Good job!
 
Comment #19  (Posted by an unknown user on 10/06/2006)
Rating
This is a Good article - introduces the subject well - flies at 10,000 feet and I enjoyed as a manager but may not be that useful, as a developer except providing a useful background.
 
Comment #20  (Posted by Assaf on 10/16/2006)
Rating
Excellent!
 
Comment #21  (Posted by an unknown user on 10/19/2006)
Rating
Good article
 
Comment #22  (Posted by Mohammad Hefny on 11/12/2006)
Rating
Very great topic...too advanced yet too clear.
I really learned important issues by reading it.
Thank you
 
Comment #23  (Posted by an unknown user on 02/01/2007)
Rating
it's interesting but is's nice to have source code available.
 
Comment #24  (Posted by an unknown user on 06/30/2007)
Rating
Very Cool Article if you have sample code then please send me at sumit.professional@gmail.com or kumar_chams@yahoo.com
 
Comment #25  (Posted by an unknown user on 08/21/2007)
Rating
The examples in your tutorial has some problems. The following Sub is not threadsafe, not commenting on the fact that the method uses a lock to write a local variable. The actual assignment is not threadsafe since each thread calling the method will create a new objLock instance and lock on that, meaning that each thread will in fact be able to get the lock, and do the assignment.

Public Sub Foo()

Dim iData as Integer
Dim objLock as Object = New Object()
SyncLock objLock
iData = 3
End SyncLock
End Sub

Not a very good example. I would think the same problem counts for the monitor examples also.
 
Comment #26  (Posted by an unknown user on 08/21/2007)
Rating
Better than any other article I have seen.
 
Comment #27  (Posted by mm on 10/31/2007)
Rating
This is an excellent article, thanks. To those who complained about the lock being acquired on a local variable - well, duh. Not like it's rocket science to figure that out....
 
Comment #28  (Posted by an unknown user on 12/13/2007)
Rating
I both agree and disagree with Comment #25.

I agree with #25 in that based on some computer generated code in one of my projects the line "Private Shared objLock As New Object" placed in the same class as the Foo() subroutine would resolve the issues that #25 has pointed out.

I disagree with #25's "Not a very good example" statement because it is both an excellent article with excellent examples. If one mistake is all it takes to make something "Not very good" then I submit to all who read this that little to nothing any has every done has ever been "very good".

 
Comment #29  (Posted by an unknown user on 12/14/2007)
Rating
Hey! You can pass values to DelegateWork with the following code:

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim Thread1 As Thread
Thread1 = New Thread(AddressOf Thread1Work)
Thread1.Start()
End Sub

Private Delegate Sub DelAddItem(ByVal StringHeader As String)
Private Sub Thread1Work()
txtList.Invoke(New DelAddItem(AddressOf DelegateWork), New Object() {"A New Line: "})
Debug.Print("Thread 1 Done")
End Sub
Private Sub DelegateWork(ByVal StringHeader As String)
Dim i As Integer
For i = 0 To 100
txtList.Text = txtList.Text & StringHeader & i.ToString() & vbCrLf
Next 'i
Debug.Print("Delegate Done")
End Sub
 
Comment #30  (Posted by an unknown user on 01/14/2008)
Rating
I have a quuestion, is there a way to implement somthing like a network mutex, where the resources lie on computer 1, and other computers sync to access the resources, please mail me matijasch@yahoo.com, the article is great only this topic is missing, thanks a lot, for helping us
 
Comment #31  (Posted by an unknown user on 01/22/2008)
Rating
brilliant... print and keep, then re-read, re-read, re-read.


 
Comment #32  (Posted by an unknown user on 02/17/2008)
Rating
Great article. Tons of good information. Covered everything that I needed. Thanks, keep writing.
 
Comment #33  (Posted by an unknown user on 02/28/2008)
Rating
Rating says it all
 
Sponsored Links