473,406 Members | 2,713 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,406 software developers and data experts.

Problem understanding Synclock

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

Synclock MyBufferClass
...........
End Synclock

My question is, what is Synlock protecting? Is it just protecting
MyBufferClass and all the members of MyBufferClass? Or could you have
code inside the sandwich that is totally unrelated like:

Synclock MyBufferClass
unrelatedvariable = "foozleberg"
End Synclock

Would "unrelatedvariable" be also protected from other processes
updating it?

Thanks,
HA

Mar 9 '07 #1
2 10025
I have a buffer that needs to be locked sometimes, because 2 processes
update it.
Synclock (and the related Monitor.Enter) apply to multiple threads in one
process. If you are really dealing with two processes, you need to use a
mutex. If you are dealing with multiple threads within one process, then
read on.
My question is, what is Synlock protecting? Is it just protecting
MyBufferClass and all the members of MyBufferClass? Or could you have
code inside the sandwich that is totally unrelated like...
Synclock (and Monotor.Enter) assure that only one thread will be executing
at a time within Synclock'ed section(s) of code. When thread1 is executing
between Synclock xxx and its matching End Synclock, thread2 will block when
it attempts to execute Synclock xxx. Thread2's block will release when
thread1 executes End Synclock. What you do within the sections of code is up
to you, so the Synclock is not really protecting anything beyond enforcing
only one Synclock 'owner' of xxx. This notion is the same as with mutexes -
all they do is assure one 'owner' of the mutex at a time.

Imagine two subs that add and remove entries from some object of yours. No
problem if your program is single threaded. If it has multiple threads, all
will be well if you Synclock appropriately, eg

Dim MyObject As New SomeObjectClassYouWrote

Sub Add(whatever)
Synclock MyObject
whatever it takes to do an add goes here
End Synclock
End sub

Sub Remove(whatever)
Synclock MyObject
whatever it takes to do a remove goes here
End Synclock
End sub

The object being Synclock'ed could be the implied object (Me) if sub Add is
a method of class SomeObjectClassYouWrote, eg

Sub Add(whatever)
Synclock Me
whatever it takes to do an add goes here
End Synclock
End sub

In this case, if you have two object instances of SomeObjectClassYouWrote,
say o1 and o2, then calling o1.Add on one thread and calling o2.Add on
another thread would proceed without any blocking. On the other hand, if
both threads called o1.Add nearly simultaneously, then one would enter the
synclock, and the other would block.
Would "unrelatedvariable" be also protected from other processes
updating it?
No, if you followed the above. All mutexes and Synclocks do is enforce
taking turns. Remember, mutexes are for processes, and Synclocks are for
threads within processes.

Mar 9 '07 #2
On Mar 9, 6:44 am, HONOREDANCES...@YAHOO.COM wrote:
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

Synclock MyBufferClass
..........
End Synclock

My question is, what is Synlock protecting? Is it just protecting
MyBufferClass and all the members of MyBufferClass? Or could you have
code inside the sandwich that is totally unrelated like:

Synclock MyBufferClass
unrelatedvariable = "foozleberg"
End Synclock

Would "unrelatedvariable" be also protected from other processes
updating it?

Thanks,
HA
Hi,

SyncLock will not synchronize anything between processes. SyncLock is
based on the Monitor class which usually operates on AppDomain
specific objects. You could use domain neutral objects as targets for
the Monitor to synchronize across domains, but that's pretty rare.

The Monitor.Enter method (which SyncLock calls for you) accepts an
object that will be used as a monitor. It's important to understand
that it isn't the monitor object that's being synchronized. The
monitor object just acts as guard. Everything in the SyncLock block
is protected from other threads that use a SyncLock on the *same*
monitor object.

If you're really needing to synchronize access to a buffer that is
shared between processes then you could use a Mutex.

Brian


Mar 9 '07 #3

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

Similar topics

7
by: Jason Callas | last post by:
I have a process that manages a list of trades. Each trade is a separate row in the table. I blast the process with 10,000 new trades in order to stress test it and end up with the below listed...
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...
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: Ramta | last post by:
Hi all, I am trying to develop a Producer thread that listens for UDP packets on a socket and store then in an ArrayList. The consumer should read data from list and block when no element is...
2
by: shipcreak | last post by:
I have an interesting problem with a sort of producer-consumer system for error logging. Consider the following code: <code> SyncLock _eventList.SyncRoot Dim item As ExceptionLogEntry '...
5
by: nondos | last post by:
Hello i have problem with thread, I have Form with listview and User control. I need the User control to run thread that will clear the listview in the main form. the code work but it's not...
0
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
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
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,...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.