473,378 Members | 1,555 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,378 software developers and data experts.

Synclock Questions

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 shared class

Private Shared gcCDsd As New Class_Component_Designer
Private Shared Sub tblMETHODS_DETAILSC_ADD_Row(ByVal
tblMethods_Summary_Row_Number_0_Based As Integer, ByVal Summary_Text As
String, Optional ByVal Details_Text As String = "", Optional ByVal
Exception_Thrown_Message As String = "")

****** Question 0) The same as question 2 below for the arguments above.

... code to create row to add

SyncLock (gcCDsd.DS_Methods.Methods_Details)

' Add new DataRow to DataSet

gcCDsd.DS_Methods.Methods_Details.AddMethods_Detai lsRow(DR_To_Add)

' Call method to update row to datasourse

Rows_Updated = DLsd.tblMETHODS_DETAILS_UPDATE()

End SyncLock

end sub

Private Shared Function tblMETHODS_DETAILS_UPDATE() As Integer

' Purpose: Updates the DataSource From the DataSet for this table only

****** Question 1) Since gcCDsd.DS_Methods.Methods_Details is synclocked in
the calling method, does it need to be synclocked again here. I think not,
since it would seem to be already locked. If so, it would seem that the
same thread running into the same item with a 2nd synclock on it would
ignore the 2nd synclock. Is that thinking correct?

' Holds Rows affected by DataAdapter commands, such as FILL or UPDATED

Dim Rows_Updated As Integer = -1

' call method to update datasource

Rows_Updated = DL.cmdUPDATE_DataSource(gcCDsd.DA_tblMethods_Detai ls,
gcCDsd.DS_Methods.Methods_Details)

' return rows updated

Return Rows_Updated

End Function

Friend Shared Function cmdUPDATE_DataSource(ByVal SQL_Data_Adapter As
SqlDataAdapter, ByVal DataTable_To_Update As DataTable) As Integer

' Purpose: Updates any changes made to a dataset datatable to the
datasource

****** Question 2) Do the parameters passed above need to be synclocked?
In other words, if 2 threads call this, do the arguments above instantiate a
new variable each time it is called (like dim SQL_Data_Adapter As
SqlDataAdapter would) or do they amount to shared variables that must be
protected by synclocks? It worked with out synclocks with 2 threads, but as
I have added threads I have had problems.

' Holds number of DS rows Updated to DataSource

Dim Rows_Updated As Integer = -1

****** Question 3) You would never synclock the next DIM line, since each
pass by a different thread (even at the same time) would create its own
DT_With_Changes to work with.

' create DataTable to hold only the changes in table

Dim DT_With_Changes As New DataTable

' load with rows that have changes in table

DT_With_Changes = DataTable_To_Update.GetChanges()

Rows_Updated = SQL_Data_Adapter.Update(DT_With_Changes)

' all rows for this table set back to unmodified.

DataTable_To_Update.AcceptChanges()

' Return number of rows updated6

Return Rows_Updated

End Function
Thanks!

Bob Day


Nov 20 '05 #1
10 3135
****** Question 1) Since gcCDsd.DS_Methods.Methods_Details is synclocked in the calling method, does it need to be synclocked again here. I think not, since it would seem to be already locked. If so, it would seem that the
same thread running into the same item with a 2nd synclock on it would
ignore the 2nd synclock. Is that thinking correct?
You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows
one thread through the gate at a time.
The variable that you use in the synclock is kind of like a ticket to get
through the gate, but there is (should be) only one ticket.
The first thread to come to the synclock statement "grabs" the ticket. The
bouncer at the front gate sees he has the ticket and lets him through. The
thread then gives up the ticket at the back gate (the end of the synclock
block). The second thread must wait in line until he can get his hands on
the ticket.
I would recommend creating a seperate variable used for locking.
****** Question 2) Do the parameters passed above need to be synclocked?
In other words, if 2 threads call this, do the arguments above instantiate a new variable each time it is called (like dim SQL_Data_Adapter As
SqlDataAdapter would) or do they amount to shared variables that must be
protected by synclocks? It worked with out synclocks with 2 threads, but as I have added threads I have had problems.
All arguments get a seperate copy for each thread as they are placed on the
stack, and stacks are managed on a per-thread basis. However, what you have
as arguments here are object references (in essense pointers to some object
that lives on the heap). Getting multiple copies of pointers doesn't matter
because you are referencing the same objects on the heap, which might be
doing something at the time any particular thread is trying to use it.
Synclock this procudure to prevent that from happening (remember, you
synclocking code, not variables).
****** Question 3) You would never synclock the next DIM line, since each
pass by a different thread (even at the same time) would create its own
DT_With_Changes to work with.


Once again, you synclock code, not variables :-)
However, you are right in saying that locally-scoped variables get created
unique to the current thread, so you don't need to worry about them, unless
they are references to a common object on the heap. In this case not,
because each thread is creating a NEW object.

-Rob Teixeira [MVP]
Nov 20 '05 #2
With all due respect, you are giving me to question 1 the exact opposite
answer I have been previously given in this newsgroup. Previous, it was
stated that you don't sysnclock code, you synclock shared or global
varialbles, modify them, end sycnlock , since those variables can be
modified by another thread.

You are saying create an arbitrary variable, and synclock all the code you
want (whether it pertains to shared or global variables or not), not
worrying about shared or global variables, like the follwing:

Private shared sub ToSomething
dim x as string
synclock x
..all code
end synclock
End Sub

Your approach seems easier, but by the vary nature of creating an arbitrary
variable to use synclock, that does not seem how synclock was intended to be
used.

Please advise. Also, can you provide soem URLs on your approach?

Thansk!
Bob

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uU*************@TK2MSFTNGP09.phx.gbl...
****** Question 1) Since gcCDsd.DS_Methods.Methods_Details is synclocked in
the calling method, does it need to be synclocked again here. I think

not,
since it would seem to be already locked. If so, it would seem that the
same thread running into the same item with a 2nd synclock on it would
ignore the 2nd synclock. Is that thinking correct?


You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows
one thread through the gate at a time.
The variable that you use in the synclock is kind of like a ticket to get
through the gate, but there is (should be) only one ticket.
The first thread to come to the synclock statement "grabs" the ticket. The
bouncer at the front gate sees he has the ticket and lets him through. The
thread then gives up the ticket at the back gate (the end of the synclock
block). The second thread must wait in line until he can get his hands on
the ticket.
I would recommend creating a seperate variable used for locking.
****** Question 2) Do the parameters passed above need to be synclocked? In other words, if 2 threads call this, do the arguments above instantiate a
new variable each time it is called (like dim SQL_Data_Adapter As
SqlDataAdapter would) or do they amount to shared variables that must be
protected by synclocks? It worked with out synclocks with 2 threads,
but as
I have added threads I have had problems.
All arguments get a seperate copy for each thread as they are placed on

the stack, and stacks are managed on a per-thread basis. However, what you have as arguments here are object references (in essense pointers to some object that lives on the heap). Getting multiple copies of pointers doesn't matter because you are referencing the same objects on the heap, which might be
doing something at the time any particular thread is trying to use it.
Synclock this procudure to prevent that from happening (remember, you
synclocking code, not variables).
****** Question 3) You would never synclock the next DIM line, since
each pass by a different thread (even at the same time) would create its own
DT_With_Changes to work with.


Once again, you synclock code, not variables :-)
However, you are right in saying that locally-scoped variables get created
unique to the current thread, so you don't need to worry about them,

unless they are references to a common object on the heap. In this case not,
because each thread is creating a NEW object.

-Rob Teixeira [MVP]

Nov 20 '05 #3
You don't want to use a local variable as your sync mechanism.
Synclock creates a barrier around reentrant code (code that can be accessed
by multiple threads) to prevent multiple threads from executing that code at
the same time. The object you use inside the parens of the Synclock
statement is mearly used as a ticket to get into the synclocked code
barrier. Only one thread can "grab" that ticket at a time.
Most early examples used a class' Type object as this ticket, but that has
since proven to be a bad idea. You can read more about that here:
http://msdn.microsoft.com/library/de...ui06032003.asp

-Rob Teixeira [MVP]

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
With all due respect, you are giving me to question 1 the exact opposite
answer I have been previously given in this newsgroup. Previous, it was
stated that you don't sysnclock code, you synclock shared or global
varialbles, modify them, end sycnlock , since those variables can be
modified by another thread.

You are saying create an arbitrary variable, and synclock all the code you
want (whether it pertains to shared or global variables or not), not
worrying about shared or global variables, like the follwing:

Private shared sub ToSomething
dim x as string
synclock x
..all code
end synclock
End Sub

Your approach seems easier, but by the vary nature of creating an arbitrary variable to use synclock, that does not seem how synclock was intended to be used.

Please advise. Also, can you provide soem URLs on your approach?

Thansk!
Bob

Nov 20 '05 #4
Rob,
You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows
one thread through the gate at a time. Are you sure about this?

Yes the SyncLock itself is around a block of code, however is not its real
intent to protect one or more 'shared' variables?

In other words why protect a block of code that does not reference 'shared'
variables?

By 'shared' variable I mean an value (reference type or value type) that can
be accessed by two threads simultaneously. Either a field declared in a
Module, in a Class with the Shared keyword, or an instance field in a Class
that the object itself is referenced by two threads.

I know what you mean by the above statement, I want to make sure that Bob
Day knows what you mean by the above statement!

Unfortunately I can't really explain it any better then I already have. (the
previous thread Bob Day mentions).

Hope this helps
Jay
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:uU*************@TK2MSFTNGP09.phx.gbl...
****** Question 1) Since gcCDsd.DS_Methods.Methods_Details is synclocked in
the calling method, does it need to be synclocked again here. I think

not,
since it would seem to be already locked. If so, it would seem that the
same thread running into the same item with a 2nd synclock on it would
ignore the 2nd synclock. Is that thinking correct?


You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows
one thread through the gate at a time.
The variable that you use in the synclock is kind of like a ticket to get
through the gate, but there is (should be) only one ticket.
The first thread to come to the synclock statement "grabs" the ticket. The
bouncer at the front gate sees he has the ticket and lets him through. The
thread then gives up the ticket at the back gate (the end of the synclock
block). The second thread must wait in line until he can get his hands on
the ticket.
I would recommend creating a seperate variable used for locking.
****** Question 2) Do the parameters passed above need to be synclocked? In other words, if 2 threads call this, do the arguments above instantiate a
new variable each time it is called (like dim SQL_Data_Adapter As
SqlDataAdapter would) or do they amount to shared variables that must be
protected by synclocks? It worked with out synclocks with 2 threads,
but as
I have added threads I have had problems.
All arguments get a seperate copy for each thread as they are placed on

the stack, and stacks are managed on a per-thread basis. However, what you have as arguments here are object references (in essense pointers to some object that lives on the heap). Getting multiple copies of pointers doesn't matter because you are referencing the same objects on the heap, which might be
doing something at the time any particular thread is trying to use it.
Synclock this procudure to prevent that from happening (remember, you
synclocking code, not variables).
****** Question 3) You would never synclock the next DIM line, since
each pass by a different thread (even at the same time) would create its own
DT_With_Changes to work with.


Once again, you synclock code, not variables :-)
However, you are right in saying that locally-scoped variables get created
unique to the current thread, so you don't need to worry about them,

unless they are references to a common object on the heap. In this case not,
because each thread is creating a NEW object.

-Rob Teixeira [MVP]

Nov 20 '05 #5
In article <Oo**************@TK2MSFTNGP10.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
Rob,
You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows
one thread through the gate at a time. Are you sure about this?


Rob is correct, IMHO.
Yes the SyncLock itself is around a block of code, however is not its real
intent to protect one or more 'shared' variables?

It's intent is to create a critical section to protect any shared
resource. That may not necessarily be a shared/member variable.
In other words why protect a block of code that does not reference 'shared'
variables?


Two threads writing to a file... They may not share a variable, but
they may share access to the file. Its about shared resources.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #6

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
Rob,
You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows one thread through the gate at a time. Are you sure about this?


Sure am :-)
Yes the SyncLock itself is around a block of code, however is not its real
intent to protect one or more 'shared' variables?
Not necessarily. Its purpose is to protect reentrant code from multiple
simultaneous threads of execution (and its quite likely that what you might
indirectly be protecting is a shared variable that should only be accessed
by one thread at a time).
In other words why protect a block of code that does not reference 'shared' variables?
It might be trying to access a system resource that shouldn't be accessed by
simultaneous threads of execution?
By 'shared' variable I mean an value (reference type or value type) that can be accessed by two threads simultaneously. Either a field declared in a
Module, in a Class with the Shared keyword, or an instance field in a Class that the object itself is referenced by two threads.

I know what you mean by the above statement, I want to make sure that Bob
Day knows what you mean by the above statement!

Unfortunately I can't really explain it any better then I already have. (the previous thread Bob Day mentions).
Check out my other post to Bob.
Let's say, for example, that you have a shared object. And, you know there's
nothing wrong with accessing (reading) one of its properties regardless of
how many threads are trying to do that simultaneously. However, you don't
want simultaneous calls to this object's "ChangeCriticalStuff" method. You
don't want to lock the whole object, just the critical reenterant method.
Does that help?

Of course, there are many more variety of situations you have to account for
when dealing with multi-threading, so don't take this one example as the
only thing that needs consideration.

-Rob Teixeira [MVP]
Hope this helps
Jay

Nov 20 '05 #7
Rob,
You need to make sure it helps Bob Day!

As I totally agree with what you & Tom are saying, I'm just trying to ensure
you say it in a way that Bob Day understands!

Its is not worth my time to "split hairs" with you!

Jay

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
Rob,
You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows one thread through the gate at a time. Are you sure about this?


Sure am :-)
Yes the SyncLock itself is around a block of code, however is not its real intent to protect one or more 'shared' variables?


Not necessarily. Its purpose is to protect reentrant code from multiple
simultaneous threads of execution (and its quite likely that what you

might indirectly be protecting is a shared variable that should only be accessed
by one thread at a time).
In other words why protect a block of code that does not reference 'shared'
variables?


It might be trying to access a system resource that shouldn't be accessed

by simultaneous threads of execution?
By 'shared' variable I mean an value (reference type or value type) that can
be accessed by two threads simultaneously. Either a field declared in a
Module, in a Class with the Shared keyword, or an instance field in a

Class
that the object itself is referenced by two threads.

I know what you mean by the above statement, I want to make sure that Bob Day knows what you mean by the above statement!

Unfortunately I can't really explain it any better then I already have.

(the
previous thread Bob Day mentions).


Check out my other post to Bob.
Let's say, for example, that you have a shared object. And, you know

there's nothing wrong with accessing (reading) one of its properties regardless of
how many threads are trying to do that simultaneously. However, you don't
want simultaneous calls to this object's "ChangeCriticalStuff" method. You
don't want to lock the whole object, just the critical reenterant method.
Does that help?

Of course, there are many more variety of situations you have to account for when dealing with multi-threading, so don't take this one example as the
only thing that needs consideration.

-Rob Teixeira [MVP]
Hope this helps
Jay


Nov 20 '05 #8
Tom,
You need to make sure it helps Bob Day!

As I totally agree with what you & Tom are saying, I'm just trying to ensure
you say it in a way that Bob Day understands!

Its is not worth my time to "split hairs" with you!

Jay

"Tom Shelton" <to*@mtogden.com> wrote in message
news:ub**************@TK2MSFTNGP12.phx.gbl...
In article <Oo**************@TK2MSFTNGP10.phx.gbl>, Jay B. Harlow [MVP -

Outlook] wrote:
Rob,
You don't synclock variables, you synclock blocks of code.
Synclock establishes a walled gate around a piece of code and only allows one thread through the gate at a time.

Are you sure about this?


Rob is correct, IMHO.
Yes the SyncLock itself is around a block of code, however is not its real intent to protect one or more 'shared' variables?


It's intent is to create a critical section to protect any shared
resource. That may not necessarily be a shared/member variable.
In other words why protect a block of code that does not reference 'shared' variables?


Two threads writing to a file... They may not share a variable, but
they may share access to the file. Its about shared resources.

--
Tom Shelton
MVP [Visual Basic]

Nov 20 '05 #9
Not trying to split anything with anyone :-)
I'm just under the impression that Bob has been led to believe that if he
uses a variable in the parens of the synclock statement, it will lock that
variable, which isn't entirely correct.
I hope he got the meaning of what I was saying :-)

-Rob Teixeira [MVP]

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e2**************@tk2msftngp13.phx.gbl...
Rob,
You need to make sure it helps Bob Day!

As I totally agree with what you & Tom are saying, I'm just trying to ensure you say it in a way that Bob Day understands!

Its is not worth my time to "split hairs" with you!

Jay

Nov 20 '05 #10
On 2004-01-01, Jay B. Harlow [MVP - Outlook] <Ja************@msn.com> wrote:
Tom,
You need to make sure it helps Bob Day!

I agree. I hope it does help him. I just don't want Bob to have the
wrong impression as to what a SyncLock is about...
As I totally agree with what you & Tom are saying, I'm just trying to ensure
you say it in a way that Bob Day understands!

Understood.
Its is not worth my time to "split hairs" with you!


I'm not trying to split hairs - it's just that as Rob said, Bob seems to
have a misunderstanding about the intent of SyncLock. I thought Robs,
explanation was right on...

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #11

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...
4
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...
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
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...
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.