Click to See Complete Forum and Search --> : [RESOLVED] Sync Lock question VB 2005
DataMiser
May 16th, 2010, 11:20 AM
I have been reading through lots of sample code and explainations regaurding the uses of Sync lock in multithreaded programs but still am a little fuzzy on this.
Am I correct in assuming that if I have two or more threads running the same procedure which has a sync lock on a shared resource that the first thread to call the lock will complete its update of the resource and any of the other threads to try and lock this object would just wait in line until it is released by the first object?
I am also a bit fuzzy on what is actually locked by the sync lock.
Lets say I have code that uses sync lock and the object specified is the current thread. Does that also lock the shared resources that are referenced within the sync lock block or would I need to use another method to do this?
What if my shared resourses were all in another class and I issue a sync lock on that class before I try to modify the resources would that be the proper way to go about it?
And last but not least is there any need to use sync lock when the shared resource is in effect read only [meaning the program will read the value but will not change it during runtime]
Any info would be helpful.
dglienna
May 17th, 2010, 07:33 PM
Sync Lock's are called on Threads, not classes.
A Lock blocks all other threads, and queues them up to fire sequential. when whatever locked the thread releases the lock. You can also synchronize a release to more than one thread
DataMiser
May 17th, 2010, 07:48 PM
hmm. Examples, tutorials that I have saw so far [ I have looked at quite a few] Almost all either point to using me or creating a new object and using that. I have yet to find one that uses the Thread as the object on the synclock however that is what I am doing and it does seem to be working. Difficult to tell for sure what is going on at this time in the program though.
I have also tried calling the synclock on my shared class object and that seems to work as well.
Can you explain a bit on what you mean by synchronize a release to more than one thread?
In my case I could have anywhere from 1 to 25 threads [or possibly more] running basically the same code all of which will need to update certian shared variables at some point.
DataMiser
May 17th, 2010, 07:51 PM
Ideally I would like to lock only when I am accessing these shared variables. This will happen at the start of the thread and the end of the thread. I don;t really want to lock any other thread that is not at this point in the code if it can be avoided as they would not be accessing these resources at the time.
Also what about reading from a var. Does this need to be locked as well or is it fine to read from more than one thread without a lock?
dglienna
May 18th, 2010, 12:55 AM
This is pretty good: http://www.albahari.com/threading/
DataMiser
May 19th, 2010, 08:09 AM
Thanks. I think I have a better understanding of this now.
It seems that in my case I should create a new object for the lock. If I understand this correctly when the code gets to the lock if that object is in use it will wait until it can be locked to proceed. This should allow me to update shared resources in one or more classes from these new threads without fear of stepping on one another.
Since this object will only be used for locking then it should not effect any thread that is not at the point of needing to lock this object.
Can anyone confirm that this is correct?
dglienna
May 19th, 2010, 12:11 PM
Anything that access the object will Wait()
HanneSThEGreaT
May 19th, 2010, 12:33 PM
Thanks. I think I have a better understanding of this now.
It seems that in my case I should create a new object for the lock. If I understand this correctly when the code gets to the lock if that object is in use it will wait until it can be locked to proceed. This should allow me to update shared resources in one or more classes from these new threads without fear of stepping on one another.
Since this object will only be used for locking then it should not effect any thread that is not at the point of needing to lock this object.
That is how I understand it as well :)
DataMiser
May 19th, 2010, 02:10 PM
I have been running some tests and so far it looks like everything is working ok.
I added at the top of my class
Private ThreadLocker As New Object
Then inside of my data arrival routine I have added
SyncLock ThreadLocker
cInfo.LastReceived = ClientSocket.RemoteEndPoint.ToString & " -> " & TransactionString
cInfo.TotalBytesSent += BytesSent
cInfo.TotalBytesReceived += BytesReceived
cInfo.LastSent = ClientSocket.RemoteEndPoint.ToString & " <- " & ReturnString
End SyncLock
This routine runs in a seperate thread from the main class and can have any number of these threads running at once.
My testing invloves 4 clients that are sending transactions to be processed and returned as well as one other client which is a monitor program that will report infomation contained in the cinfo class via a gui.
I have set my monitor to poll the server every 200ms the 4 clients are sometimes running staggered and other times hitting the server together or at least very very close together.
I used a timer set to 500ms and a counter var. In the timer function I use a mod function to determine which client should send. based on Mod 2,3,4,5 so in some cases only 1 will send in others 2, 3 or occasionally all of them will send in rapid succession.
So far I have let a few thousand transactions go through and all seem to be working so I think I have it.
Thanks
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.