473,466 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

synclock question

SD
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 have?

Thanks
SD

Code in Module

public A as Collection
Code in first file (this is a sub for a thread)
public sub1()

synclock A
A.add(.....)
end synclock

end sub
Code in another sub in another file for another thread

public sub2()

synclock A
'perform some operations on A
end synclock

end sub
Nov 21 '05 #1
7 1612
SD,

When I understand you well, than is that where Synclock is made for, be
aware that Synclock should be avoided as much as possible.

http://msdn.microsoft.com/library/de...tmsynclock.asp

I hope this helps?

Cor

"SD" <un*****@hotmail.com>
..
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 have?

Thanks
SD

Code in Module

public A as Collection
Code in first file (this is a sub for a thread)
public sub1()

synclock A
A.add(.....)
end synclock

end sub
Code in another sub in another file for another thread

public sub2()

synclock A
'perform some operations on A
end synclock

end sub

Nov 21 '05 #2

Yes the this will work BUT variable A is has Public scope so any procedure
can change it not just those in the module where it is defined. This means
that some other piece of code outside of the module could access it without
a SyncLock and change it even if a thread has a SyncLock on it using one of
the module's thread safe procedures. This would have unexpected results.
Some further thought maybe required in the design of this portion of your
assembly.

--Robby
"SD" <un*****@hotmail.com> wrote in message
news:OK**************@TK2MSFTNGP14.phx.gbl...
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 have?

Thanks
SD

Code in Module

public A as Collection
Code in first file (this is a sub for a thread)
public sub1()

synclock A
A.add(.....)
end synclock

end sub
Code in another sub in another file for another thread

public sub2()

synclock A
'perform some operations on A
end synclock

end sub

Nov 21 '05 #3
SD
Thanks for the replies. I have made sure that every place where A is
accessed is in SyncLock blocks. But after reading more about SyncLock I
came across conflicting opinions as to the working of SyncLock. Some
said that SyncLock can only be used to Sync a block of code. Here I'm
attempting to sync several blocks of code by locking on one variable.

Robby wrote:
Yes the this will work BUT variable A is has Public scope so any procedure
can change it not just those in the module where it is defined. This means
that some other piece of code outside of the module could access it without
a SyncLock and change it even if a thread has a SyncLock on it using one of
the module's thread safe procedures. This would have unexpected results.
Some further thought maybe required in the design of this portion of your
assembly.

--Robby
"SD" <un*****@hotmail.com> wrote in message
news:OK**************@TK2MSFTNGP14.phx.gbl...
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 have?

Thanks
SD

Code in Module

public A as Collection
Code in first file (this is a sub for a thread)
public sub1()

synclock A
A.add(.....)
end synclock

end sub
Code in another sub in another file for another thread

public sub2()

synclock A
'perform some operations on A
end synclock

end sub


Nov 21 '05 #4
SD
Cor Ligthert wrote:
SD,

When I understand you well, than is that where Synclock is made for, be
aware that Synclock should be avoided as much as possible.


And what should I use instead?
Nov 21 '05 #5
SD,

When you need it, use it, however do not create it everywhere just because
that is easier or you are in doubt..

As alternative I try to keep values local in the thread and send the
resulting values to the main thread raising an event from that worker
thread.

However that is not always possible of course.

I hope this helps?

Cor

"SD" <un*****@hotmail.com>
Cor Ligthert wrote:
SD,

When I understand you well, than is that where Synclock is made for, be
aware that Synclock should be avoided as much as possible.


And what should I use instead?

Nov 21 '05 #6
You are correct in stating "SyncLock can only be used to Sync a block of
code". This puts no limitions on how many SyncLocks you can put in your
code. You are SyncLocking two different pieces of code with two separate
SyncLocks using the same locking object. This is what SyncLock is designed
for and your use of it is perfectly legal and within the .Net guidelines.

--Robby

"SD" <un*****@hotmail.com> wrote in message
news:OD*************@TK2MSFTNGP11.phx.gbl...
Thanks for the replies. I have made sure that every place where A is
accessed is in SyncLock blocks. But after reading more about SyncLock I
came across conflicting opinions as to the working of SyncLock. Some said
that SyncLock can only be used to Sync a block of code. Here I'm
attempting to sync several blocks of code by locking on one variable.

Robby wrote:
Yes the this will work BUT variable A is has Public scope so any
procedure can change it not just those in the module where it is
defined. This means that some other piece of code outside of the module
could access it without a SyncLock and change it even if a thread has a
SyncLock on it using one of the module's thread safe procedures. This
would have unexpected results. Some further thought maybe required in the
design of this portion of your assembly.

--Robby
"SD" <un*****@hotmail.com> wrote in message
news:OK**************@TK2MSFTNGP14.phx.gbl...
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 have?

Thanks
SD

Code in Module

public A as Collection
Code in first file (this is a sub for a thread)
public sub1()

synclock A
A.add(.....)
end synclock

end sub
Code in another sub in another file for another thread

public sub2()

synclock A
'perform some operations on A
end synclock

end sub



Nov 21 '05 #7
SD
Robby wrote:
You are correct in stating "SyncLock can only be used to Sync a block of
code". This puts no limitions on how many SyncLocks you can put in your
code. You are SyncLocking two different pieces of code with two separate
SyncLocks using the same locking object. This is what SyncLock is designed
for and your use of it is perfectly legal and within the .Net guidelines.

--Robby

Thanks!
Nov 21 '05 #8

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

Similar topics

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...
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...
6
by: Johan Karlsson | last post by:
Hi all! I just need a true or false answer for this statement. Consider having a piece of code that boils down to this Dim a = new ArrayList SyncLock a SyncLock a a.Add("Something") End...
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...
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: 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: buu | last post by:
If I have a code like: TRY SYNCLOCK Object1 //some code END SYNCLOCK CATCH END TRY
4
by: Gangreen | last post by:
there is a shared datastructure I'm using, and I want to make sure no 2 threads are altering it at the same time. I have something like: Sub1 Synclock Locker do something...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.