473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SyncLock

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 or do the threads queue
in a FIFO or perhaps a LIFO bases.

Thanks
Fred
Sub SomeWork()
SyncLock Me.GetType
...Code here
End SyncLock
End Sub
Nov 20 '05 #1
4 3759
It uses a FIFO Queue.
"fred" <So***@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 or do the threads queue in a FIFO or perhaps a LIFO bases.

Thanks
Fred
Sub SomeWork()
SyncLock Me.GetType
...Code here
End SyncLock
End Sub

Nov 20 '05 #2
Fred,
I don't have links handy, however I have seen numerous recommendations that
you should NOT use Me.GetType to lock on, that instead you should create an
explicit "padlock" variable. The biggest reason being is you may lock on
Me.GetType for one reason, while another section of code my attempt to lock
on Me.GetType for a different reason and you actually introduced a deadlock!
Also Me.GetType is not instance sensitive. All instances of the class will
block on the type, which most of the time you only need to lock on the class
instance...

The easiest way to create a "padlock" is a new object, literally "new
object"

private readonly m_padlock As New Object()
Sub SomeWork()
SyncLock m_padlock
...Code here
End SyncLock
End Sub
You can make m_padlock Shared if you need it to be across instances, however
IMHO most of the time you do not need Shared padlocks, as you want each
instance of a class to be able to run on its own thread.

Hope this helps
Jay

"fred" <So***@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... 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 or do the threads queue in a FIFO or perhaps a LIFO bases.

Thanks
Fred
Sub SomeWork()
SyncLock Me.GetType
...Code here
End SyncLock
End Sub

Nov 20 '05 #3
Thanks for the advice Jay. I will take your advice but I am not sure I
understand why.

Am I right in thinking that it can only be a problem if the program running
between the SyncLock and End SyncLock has a possibility of recalling the
"SomeWork" sub or running another SyncLock Me.GetType somewhere.

Thanks
Fred

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Of*************@tk2msftngp13.phx.gbl...
Fred,
I don't have links handy, however I have seen numerous recommendations that you should NOT use Me.GetType to lock on, that instead you should create an explicit "padlock" variable. The biggest reason being is you may lock on
Me.GetType for one reason, while another section of code my attempt to lock on Me.GetType for a different reason and you actually introduced a deadlock! Also Me.GetType is not instance sensitive. All instances of the class will
block on the type, which most of the time you only need to lock on the class instance...

The easiest way to create a "padlock" is a new object, literally "new
object"

private readonly m_padlock As New Object()
Sub SomeWork()
SyncLock m_padlock
...Code here
End SyncLock
End Sub
You can make m_padlock Shared if you need it to be across instances,

however IMHO most of the time you do not need Shared padlocks, as you want each
instance of a class to be able to run on its own thread.

Hope this helps
Jay

"fred" <So***@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 or do the threads

queue
in a FIFO or perhaps a LIFO bases.

Thanks
Fred
Sub SomeWork()
SyncLock Me.GetType
...Code here
End SyncLock
End Sub


Nov 20 '05 #4
Fred,
Am I right in thinking that it can only be a problem if the program running between the SyncLock and End SyncLock has a possibility of recalling the
"SomeWork" sub or running another SyncLock Me.GetType somewhere. No!

SyncLock is NOT needed if the same thread can call the SomeWork method while
the SomeWork method is executing, as that is simple recursion.

SyncLock is needed if two threads can call SomeWork for the same object, at
the same time, if SomeWork modifies that object.

SyncLock is NOT needed if SomeWork can be called for different objects and
you are encapsulating your data within an object.

SyncLock is needed when using "global" variables (Module, Shared Class, or
Shared Structure variables).

SyncLock is also needed if two threads both are calling methods, properties
or using fields on the same object, at the same time. Normally its better to
encapsulate the SyncLock internal to the object itself.

Of course there are exceptions to the above broad rules, and there may be
better locking mechanisms in System.Threading depending on what you are
trying to accomplish. Plus I may have missed one or two rules...

SyncLock is generally used to protect an object not just a single routine.
For example SomeWork may be updating a total amount, other routines that
read the total amount probably should wait until the update is finished...

To better answer your question it really depends on what the SomeWork
routine is doing, and how any shared or instance variables that SomeWork
uses are being used elsewhere.

Hope this helps
Jay

"Fred" <leavemealone@home> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl... Thanks for the advice Jay. I will take your advice but I am not sure I
understand why.

Am I right in thinking that it can only be a problem if the program running between the SyncLock and End SyncLock has a possibility of recalling the
"SomeWork" sub or running another SyncLock Me.GetType somewhere.

Thanks
Fred

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Of*************@tk2msftngp13.phx.gbl...
Fred,
I don't have links handy, however I have seen numerous recommendations

that
you should NOT use Me.GetType to lock on, that instead you should create

an
explicit "padlock" variable. The biggest reason being is you may lock on
Me.GetType for one reason, while another section of code my attempt to

lock
on Me.GetType for a different reason and you actually introduced a

deadlock!
Also Me.GetType is not instance sensitive. All instances of the class will
block on the type, which most of the time you only need to lock on the

class
instance...

The easiest way to create a "padlock" is a new object, literally "new
object"

private readonly m_padlock As New Object()
Sub SomeWork()
SyncLock m_padlock
...Code here
End SyncLock
End Sub


You can make m_padlock Shared if you need it to be across instances,

however
IMHO most of the time you do not need Shared padlocks, as you want each
instance of a class to be able to run on its own thread.

Hope this helps
Jay

"fred" <So***@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 or do the threads

queue
in a FIFO or perhaps a LIFO bases.

Thanks
Fred
Sub SomeWork()
SyncLock Me.GetType
...Code here
End SyncLock
End Sub



Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
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...
10
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...
3
by: Bob Day | last post by:
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,...
4
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory...
7
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...
1
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...
3
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...
2
by: j3ko | last post by:
Hi, I'm trying to start a thread that constantly iterates through an arraylist of items that the main thread adds and removes from...how would I accomplish this? Here's the gist of what I have:...
2
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.