Ok, I have done a lot of reading(of the newsgroup answers, help files and
MSDN articles) of synclock.
I understand what you are saying in the newsgroup, and it is very helpful.
It does, however, directly contradict what the VS 2003 help file says, and
it seems that various posters to my original questions on Synclock disagree
with each other. It seems, that the best practice is to use sysnlock to
protect code, not variables. That generates further questions.
Consider: Sub1 locks a section of code, not the global_variable (this is, I
think, the best practice on how it should be done). Sub 2 locks the global
variable. Now consider sub1A. If Sub1 and Sub1A are called by different
threads, it would seem to be that the value of Global_Varialbe would be
unpredictable, because while the code is locked in each sub, the varialbe it
not, so two different threads could change it at the exact same moment. Now
conisder 2B, since the variablle is locked, the results would be
predictable. Two threads could not modify the Global_Varialbe at the same
time (one would proceed the other).
Your thoughts? Thanks!
Bob Day
Private Sub1
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1
Private Sub1A
' different code, but ultimatly also modifys Global_Variable
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1A
Private Sub2
' lock the variable itself
Synclock(Global_Variable)
Global_Variable =Local_Variable + 1
End Synclock
End sub2
Private Sub2A
' different code, but ultimatly also modifys Global_Variable
' lock the variable
Synclock(Global_Variable)
Global_Variable =Local_Varialbe + 1
End Synclock
End sub2A 3 2114
Hi Bob,
Your idea is correct basically except you must use reference type. (i.e.
Global_Variable must be a reference type in your Sub2 and Sub2A.
The type of the expression in a SyncLock statement must be a reference
type.
SyncLock Statement http://msdn.microsoft.com/library/de...us/vbls7/html/
vblrfvbspec8_5.asp
As the MSDN said,
The SyncLock block is implicitly contained by a Try statement whose Finally
block calls the Shared method System.Monitor.Exit on the expression
Use Monitor to lock objects (that is, reference types), not value types.
When you pass a value type variable to Enter, it is boxed as an object. If
you pass the same variable to Enter again, it is boxed as a separate
object, and the thread does not block. The code that Monitor is supposedly
protecting is not protected. Furthermore, when you pass the variable to
Exit, still another separate object is created. Because the object passed
to Exit is different from the object passed to Enter, Monitor throws
System.SynchronizationLockException. For details, see the conceptual topic
Monitor.
For detailed information, please take a look at the link below.
Monitor.Enter Method [Visual Basic] http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemthreadingmonitorclassentertopic.asp
If you have any concern on this issue, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Bob,
As Peter stated, your lock needs to be reference types, I'm adding it needs
to be outside of your subroutines!
Sub1 & Sub1A will not work as the lock is private to the subroutines. For a
lock to be effective two threads need access to it.
Sub2 & Sub2A seems non-sense to me as you are changing the lock itself, As
soon as Sub2 modified Global_Variable itself (not the object), as long as
Sub2A is not waiting on the old Global_Variable it would be able to enter
its block of code, as the new Global_Variable is not locked by Sub2 (the old
Global_Variable is the one that is locked).
I would recommend (as I have attempted before) to use a lock OUTSIDE your
subroutines, that is also distinct from the object you are protecting.
Something like:
Private Readonly Global_Lock As New Object()
Private Global_Variable As Integer Private Sub3 ' create an arbitrary variable to lock a section of code Synclock(Global_Lock) Global_Variable= Local_Variable + 1 End Synclock end
Private Sub3A ' different code, but ultimatly also modifys Global_Variable ' create an arbitrary variable to lock a section of code Synclock(Global_Lock) Global_Variable= Local_Variable + 1 End Synclock end
Notice that Sub3 & Sub3A are using a common lock Global_Lock, when Sub3 has
Global_Lock locked it is able to modify the "shared resource" of
Global_Variable, Sub3A is not able to enter its block of code as Sub3
currently owns Global_Lock, when Sub3 reaches its End Synclock, Sub3A will
be able to enter its Synclock block and use Global_Variable.
Hope this helps
Jay
"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl... Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock.
I understand what you are saying in the newsgroup, and it is very helpful. It does, however, directly contradict what the VS 2003 help file says, and it seems that various posters to my original questions on Synclock
disagree with each other. It seems, that the best practice is to use sysnlock to protect code, not variables. That generates further questions.
Consider: Sub1 locks a section of code, not the global_variable (this is,
I think, the best practice on how it should be done). Sub 2 locks the
global variable. Now consider sub1A. If Sub1 and Sub1A are called by different threads, it would seem to be that the value of Global_Varialbe would be unpredictable, because while the code is locked in each sub, the varialbe
it not, so two different threads could change it at the exact same moment.
Now conisder 2B, since the variablle is locked, the results would be predictable. Two threads could not modify the Global_Varialbe at the same time (one would proceed the other).
Your thoughts? Thanks!
Bob Day Private Sub1
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1
Private Sub1A
' different code, but ultimatly also modifys Global_Variable
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1A
Private Sub2
' lock the variable itself
Synclock(Global_Variable)
Global_Variable =Local_Variable + 1
End Synclock
End sub2
Private Sub2A
' different code, but ultimatly also modifys Global_Variable
' lock the variable
Synclock(Global_Variable)
Global_Variable =Local_Varialbe + 1
End Synclock
End sub2A
Alright, that makes a lot of sense. Thanks.
Bob Day
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u4*************@tk2msftngp13.phx.gbl... Bob, As Peter stated, your lock needs to be reference types, I'm adding it
needs to be outside of your subroutines!
Sub1 & Sub1A will not work as the lock is private to the subroutines. For
a lock to be effective two threads need access to it.
Sub2 & Sub2A seems non-sense to me as you are changing the lock itself, As soon as Sub2 modified Global_Variable itself (not the object), as long as Sub2A is not waiting on the old Global_Variable it would be able to enter its block of code, as the new Global_Variable is not locked by Sub2 (the
old Global_Variable is the one that is locked).
I would recommend (as I have attempted before) to use a lock OUTSIDE your subroutines, that is also distinct from the object you are protecting.
Something like:
Private Readonly Global_Lock As New Object() Private Global_Variable As Integer
Private Sub3 ' create an arbitrary variable to lock a section of code Synclock(Global_Lock) Global_Variable= Local_Variable + 1 End Synclock end
Private Sub3A ' different code, but ultimatly also modifys Global_Variable ' create an arbitrary variable to lock a section of code Synclock(Global_Lock) Global_Variable= Local_Variable + 1 End Synclock end Notice that Sub3 & Sub3A are using a common lock Global_Lock, when Sub3
has Global_Lock locked it is able to modify the "shared resource" of Global_Variable, Sub3A is not able to enter its block of code as Sub3 currently owns Global_Lock, when Sub3 reaches its End Synclock, Sub3A will be able to enter its Synclock block and use Global_Variable.
Hope this helps Jay
"Bob Day" <Bo****@TouchTalk.net> wrote in message news:eJ**************@TK2MSFTNGP11.phx.gbl... Ok, I have done a lot of reading(of the newsgroup answers, help files
and MSDN articles) of synclock.
I understand what you are saying in the newsgroup, and it is very
helpful. It does, however, directly contradict what the VS 2003 help file says,
and it seems that various posters to my original questions on Synclock disagree with each other. It seems, that the best practice is to use sysnlock to protect code, not variables. That generates further questions.
Consider: Sub1 locks a section of code, not the global_variable (this
is, I think, the best practice on how it should be done). Sub 2 locks the global variable. Now consider sub1A. If Sub1 and Sub1A are called by
different threads, it would seem to be that the value of Global_Varialbe would be unpredictable, because while the code is locked in each sub, the
varialbe it not, so two different threads could change it at the exact same moment. Now conisder 2B, since the variablle is locked, the results would be predictable. Two threads could not modify the Global_Varialbe at the
same time (one would proceed the other).
Your thoughts? Thanks!
Bob Day Private Sub1
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1
Private Sub1A
' different code, but ultimatly also modifys Global_Variable
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1A
Private Sub2
' lock the variable itself
Synclock(Global_Variable)
Global_Variable =Local_Variable + 1
End Synclock
End sub2
Private Sub2A
' different code, but ultimatly also modifys Global_Variable
' lock the variable
Synclock(Global_Variable)
Global_Variable =Local_Varialbe + 1
End Synclock
End sub2A
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Keith Langer |
last post by:
I have some questions about whether synclock is necessary in a few
different scenarios:
1) I have a Queue class which is shared between two threads. Thread 1
pushes objects onto the queue and...
|
by: fred |
last post by:
If I have multiple threads running a Sub as below then a number of threads
can be held up at the SyncLock. When it becomes free which thread goes
first. Is it just by chance which thread goes first...
|
by: Bob Day |
last post by:
Using vs 2003, vb.net sql msde..
Consider the following code snippets. See **** for questions. All are
shared and accessed by multiple threads simultaneiously.
' Instantiate per for this...
|
by: SD |
last post by:
I have a public object that I only want one thread to access at a time.
However the access to this object is not limited to one procedure. Will
SyncLock work in this case? If not what options do I...
|
by: fred |
last post by:
I have a VB application that is using MS Access as its database.
To avoid connection delays the application creates one connection to the
database at start-up and maintains that single connection...
|
by: Chris Dunaway |
last post by:
I was using a Queue object like this to create my own specialized queue
class for use with my own objects:
Public Class MySpecializedQueue
Private q As New Queue
Public Sub Enqueue(obj As...
|
by: Spam Catcher |
last post by:
Hi all,
I'm hosting a remoting service in IIS. I have a function which I only want
one concurrent access at a time. Will this prevent multiple users from
accessing the function at a paritcular...
|
by: Core |
last post by:
Hello,
I am having a class that is used to spawn new threads off. In each of
them, I have a shared function that will generate an unique id off a
shared class variable. My question is whether...
|
by: eBob.com |
last post by:
I was changing some code in a multi-threaded application today and noticed
that it was not locking where it really needed to be locking. The Sub was
already working with an array so I just stuck a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |