469,963 Members | 2,761 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Threading and ArrayList access

I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.

Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.

Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don't care about the order they
are added in, I simply want to be sure they are added.

Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?

Thanks in advance.

--
Mike
mi*********@hotmail.com
Nov 20 '05 #1
14 2364
Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn't
yet increased the element counter, and thread 2 is just about to assign the
object to the array.

Klaus

"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om...
I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.

Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.

Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don't care about the order they
are added in, I simply want to be sure they are added.

Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?

Thanks in advance.

--
Mike
mi*********@hotmail.com

Nov 20 '05 #2
Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn't
yet increased the element counter, and thread 2 is just about to assign the
object to the array.

Klaus

"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om...
I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.

Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.

Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don't care about the order they
are added in, I simply want to be sure they are added.

Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?

Thanks in advance.

--
Mike
mi*********@hotmail.com

Nov 20 '05 #3
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>...
Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn't
yet increased the element counter, and thread 2 is just about to assign the
object to the array.


Thanks for the reply.

Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..

If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?

If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?

Thanks.

--
Mike
mi*********@hotmail.com
Nov 20 '05 #4
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>...
Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn't
yet increased the element counter, and thread 2 is just about to assign the
object to the array.


Thanks for the reply.

Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..

If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?

If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?

Thanks.

--
Mike
mi*********@hotmail.com
Nov 20 '05 #5
Mike,

see inline

"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om...
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>...
Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same element of the array to set. That would be the case, if thread one hasn't yet increased the element counter, and thread 2 is just about to assign the object to the array.


Thanks for the reply.

Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..

If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?


Not at all! Well, except, if those methods you are talking about are also
SyncLocked with the same object reference, and that object hasn't been
altered in a way (which is therefore exactly what you must take care
of/implement). In your case adding or deleting elements wouldn't alter
_someList in the means of synclock-synchronisation, but if you would define
_someList=new ArrayList(blbalbal) in SubFooTest2, the object would become
useless for synchronisation. To avoid that, simply use

SyncLock me

in a class instance, because it's of course not very likely, that the
instance of Me changes while its code is being executed.
If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?


No. See explanation above.
So, implement a SyncLock Me everytime you access your array, and you're on
the safe side.
However, if you have more then one element to synchonize which are supposed
to not exclude their manipulating code from being executed at the same time,
then you'd choose another object for syncronisation (a string, for example,
with a class wide scope that isn't empty and NEVER altered while the threads
are running).

Hope that helps,

Klaus
Nov 20 '05 #6
Mike,

see inline

"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om...
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>...
Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same element of the array to set. That would be the case, if thread one hasn't yet increased the element counter, and thread 2 is just about to assign the object to the array.


Thanks for the reply.

Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..

If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?


Not at all! Well, except, if those methods you are talking about are also
SyncLocked with the same object reference, and that object hasn't been
altered in a way (which is therefore exactly what you must take care
of/implement). In your case adding or deleting elements wouldn't alter
_someList in the means of synclock-synchronisation, but if you would define
_someList=new ArrayList(blbalbal) in SubFooTest2, the object would become
useless for synchronisation. To avoid that, simply use

SyncLock me

in a class instance, because it's of course not very likely, that the
instance of Me changes while its code is being executed.
If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?


No. See explanation above.
So, implement a SyncLock Me everytime you access your array, and you're on
the safe side.
However, if you have more then one element to synchonize which are supposed
to not exclude their manipulating code from being executed at the same time,
then you'd choose another object for syncronisation (a string, for example,
with a class wide scope that isn't empty and NEVER altered while the threads
are running).

Hope that helps,

Klaus
Nov 20 '05 #7
Thanks for the response. See my questions below..
<snip>
If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?
Not at all! Well, except, if those methods you are talking about are also
SyncLocked with the same object reference, and that object hasn't been
altered in a way (which is therefore exactly what you must take care
of/implement). In your case adding or deleting elements wouldn't alter
_someList in the means of synclock-synchronisation, but if you would

define _someList=new ArrayList(blbalbal) in SubFooTest2, the object would become
useless for synchronisation. To avoid that, simply use
SyncLock me
All of my SyncLock calls within class Foo would use _someList as the locking
object, so would it be valid to say that any ArrayList method call would be
blocked until the first SyncLock on _someList is released?

If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but
still keep _someList protected.

<snip> So, implement a SyncLock Me everytime you access your array, and you're on
the safe side.
However, if you have more then one element to synchonize which are supposed to not exclude their manipulating code from being executed at the same time, then you'd choose another object for syncronisation (a string, for example, with a class wide scope that isn't empty and NEVER altered while the threads are running).


I'm a bit confused on this part. Could you elaborate?

Say I have the following code:

Public Class DataClass
Private _someList As ArrayList

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub

Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub

Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub

End Class

If I have an instance of DataClass, foo, and I have 3 threads:

Thread #1: foo.AddToList()
Thread #2: foo.EmptyList()
Thread #3: foo.DoNothingToList()

If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until it
has finished? Does AddToList() block DoNothingToList()?

That is the part I am confused about.

Also, is it acceptable to use a SyncLock block in a method, then release the
lock, do some processing, acquire a lock again, do something with the locked
code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock

' Do some other stuff...

SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub

Is this ok?

Thanks for all your help!

--
Mike Loll
mi*********@hotmail.com

Nov 20 '05 #8
Thanks for the response. See my questions below..
<snip>
If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?
Not at all! Well, except, if those methods you are talking about are also
SyncLocked with the same object reference, and that object hasn't been
altered in a way (which is therefore exactly what you must take care
of/implement). In your case adding or deleting elements wouldn't alter
_someList in the means of synclock-synchronisation, but if you would

define _someList=new ArrayList(blbalbal) in SubFooTest2, the object would become
useless for synchronisation. To avoid that, simply use
SyncLock me
All of my SyncLock calls within class Foo would use _someList as the locking
object, so would it be valid to say that any ArrayList method call would be
blocked until the first SyncLock on _someList is released?

If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but
still keep _someList protected.

<snip> So, implement a SyncLock Me everytime you access your array, and you're on
the safe side.
However, if you have more then one element to synchonize which are supposed to not exclude their manipulating code from being executed at the same time, then you'd choose another object for syncronisation (a string, for example, with a class wide scope that isn't empty and NEVER altered while the threads are running).


I'm a bit confused on this part. Could you elaborate?

Say I have the following code:

Public Class DataClass
Private _someList As ArrayList

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub

Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub

Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub

End Class

If I have an instance of DataClass, foo, and I have 3 threads:

Thread #1: foo.AddToList()
Thread #2: foo.EmptyList()
Thread #3: foo.DoNothingToList()

If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until it
has finished? Does AddToList() block DoNothingToList()?

That is the part I am confused about.

Also, is it acceptable to use a SyncLock block in a method, then release the
lock, do some processing, acquire a lock again, do something with the locked
code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock

' Do some other stuff...

SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub

Is this ok?

Thanks for all your help!

--
Mike Loll
mi*********@hotmail.com

Nov 20 '05 #9
Michael,

see inline again

"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:ev**************@TK2MSFTNGP11.phx.gbl...
Thanks for the response. See my questions below..
<snip>
All of my SyncLock calls within class Foo would use _someList as the locking object, so would it be valid to say that any ArrayList method call would be blocked until the first SyncLock on _someList is released?
No, only if that method is SyncLocked with the ArrayList, too.

If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but
still keep _someList protected.

<snip>
So, implement a SyncLock Me everytime you access your array, and you're
on the safe side.
However, if you have more then one element to synchonize which are

supposed
to not exclude their manipulating code from being executed at the same

time,
then you'd choose another object for syncronisation (a string, for

example,
with a class wide scope that isn't empty and NEVER altered while the

threads
are running).


I'm a bit confused on this part. Could you elaborate?

Say I have the following code:

Public Class DataClass
Private _someList As ArrayList

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub

Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub

Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub

End Class

If I have an instance of DataClass, foo, and I have 3 threads:

Thread #1: foo.AddToList()

Would have to wait - it's SyncLocked with Me. Thread #2: foo.EmptyList() Would have to wait - it's SyncLocked with Me. Thread #3: foo.DoNothingToList() Would not have to wait. It's using the object which is used for SyncLocking
in the other methods alright, but it's actually not embedded in a SyncLock
code block.
That's actually the strange thing about SyncLock and the object it's using
as a parameter. SyncLock *does not lock the object*. It's locking *the code*
and using the object only as a kind of tool to do its job - keep that in
mind!
If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until it
has finished? Does AddToList() block DoNothingToList()?

That is the part I am confused about.

Also, is it acceptable to use a SyncLock block in a method, then release the lock, do some processing, acquire a lock again, do something with the locked code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock

' Do some other stuff...

SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub

Is this ok? Yes, that's OK.
BTW: Just in case: Keep in mind, that you musn't access controls on a form
from a thread in a direct way (by changing their properties directly, e.g.).
Only use the "diversion" over control.invoke (see vs.help on that topic for
more info).

Thanks for all your help! You're welcome!
--
Mike Loll
mi*********@hotmail.com

Nov 20 '05 #10
Michael,

see inline again

"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:ev**************@TK2MSFTNGP11.phx.gbl...
Thanks for the response. See my questions below..
<snip>
All of my SyncLock calls within class Foo would use _someList as the locking object, so would it be valid to say that any ArrayList method call would be blocked until the first SyncLock on _someList is released?
No, only if that method is SyncLocked with the ArrayList, too.

If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but
still keep _someList protected.

<snip>
So, implement a SyncLock Me everytime you access your array, and you're
on the safe side.
However, if you have more then one element to synchonize which are

supposed
to not exclude their manipulating code from being executed at the same

time,
then you'd choose another object for syncronisation (a string, for

example,
with a class wide scope that isn't empty and NEVER altered while the

threads
are running).


I'm a bit confused on this part. Could you elaborate?

Say I have the following code:

Public Class DataClass
Private _someList As ArrayList

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub

Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub

Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub

End Class

If I have an instance of DataClass, foo, and I have 3 threads:

Thread #1: foo.AddToList()

Would have to wait - it's SyncLocked with Me. Thread #2: foo.EmptyList() Would have to wait - it's SyncLocked with Me. Thread #3: foo.DoNothingToList() Would not have to wait. It's using the object which is used for SyncLocking
in the other methods alright, but it's actually not embedded in a SyncLock
code block.
That's actually the strange thing about SyncLock and the object it's using
as a parameter. SyncLock *does not lock the object*. It's locking *the code*
and using the object only as a kind of tool to do its job - keep that in
mind!
If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until it
has finished? Does AddToList() block DoNothingToList()?

That is the part I am confused about.

Also, is it acceptable to use a SyncLock block in a method, then release the lock, do some processing, acquire a lock again, do something with the locked code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock

' Do some other stuff...

SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub

Is this ok? Yes, that's OK.
BTW: Just in case: Keep in mind, that you musn't access controls on a form
from a thread in a direct way (by changing their properties directly, e.g.).
Only use the "diversion" over control.invoke (see vs.help on that topic for
more info).

Thanks for all your help! You're welcome!
--
Mike Loll
mi*********@hotmail.com

Nov 20 '05 #11
Thanks for your reply. Assuming Thread #1 in my example always executed
first, would the outcome be as follows:

Thread #1 executes and releases SyncLock.
Thread #2 acquires synclock, executes, and releases SyncLock.
Thread #3 executes whenever it can, since it is not using SyncLock.

"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message
news:OU**************@TK2MSFTNGP11.phx.gbl...
Michael,

see inline again

"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:ev**************@TK2MSFTNGP11.phx.gbl...
Thanks for the response. See my questions below..
> <snip>
All of my SyncLock calls within class Foo would use _someList as the

locking
object, so would it be valid to say that any ArrayList method call would

be
blocked until the first SyncLock on _someList is released?


No, only if that method is SyncLocked with the ArrayList, too.

If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but still keep _someList protected.

<snip>
So, implement a SyncLock Me everytime you access your array, and
you're on the safe side.
However, if you have more then one element to synchonize which are supposed
to not exclude their manipulating code from being executed at the same

time,
then you'd choose another object for syncronisation (a string, for

example,
with a class wide scope that isn't empty and NEVER altered while the

threads
are running).


I'm a bit confused on this part. Could you elaborate?

Say I have the following code:

Public Class DataClass
Private _someList As ArrayList

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub

Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub

Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub

End Class

If I have an instance of DataClass, foo, and I have 3 threads:

Thread #1: foo.AddToList()

Would have to wait - it's SyncLocked with Me.
Thread #2: foo.EmptyList()

Would have to wait - it's SyncLocked with Me.
Thread #3: foo.DoNothingToList()

Would not have to wait. It's using the object which is used for

SyncLocking in the other methods alright, but it's actually not embedded in a SyncLock
code block.
That's actually the strange thing about SyncLock and the object it's using
as a parameter. SyncLock *does not lock the object*. It's locking *the code* and using the object only as a kind of tool to do its job - keep that in
mind!

If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until
it has finished? Does AddToList() block DoNothingToList()?

That is the part I am confused about.

Also, is it acceptable to use a SyncLock block in a method, then release

the
lock, do some processing, acquire a lock again, do something with the

locked
code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock

' Do some other stuff...

SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub

Is this ok?

Yes, that's OK.


BTW: Just in case: Keep in mind, that you musn't access controls on a form
from a thread in a direct way (by changing their properties directly,

e.g.). Only use the "diversion" over control.invoke (see vs.help on that topic for more info).

Thanks for all your help!

You're welcome!

--
Mike Loll
mi*********@hotmail.com


Nov 20 '05 #12
Thanks for your reply. Assuming Thread #1 in my example always executed
first, would the outcome be as follows:

Thread #1 executes and releases SyncLock.
Thread #2 acquires synclock, executes, and releases SyncLock.
Thread #3 executes whenever it can, since it is not using SyncLock.

"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message
news:OU**************@TK2MSFTNGP11.phx.gbl...
Michael,

see inline again

"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:ev**************@TK2MSFTNGP11.phx.gbl...
Thanks for the response. See my questions below..
> <snip>
All of my SyncLock calls within class Foo would use _someList as the

locking
object, so would it be valid to say that any ArrayList method call would

be
blocked until the first SyncLock on _someList is released?


No, only if that method is SyncLocked with the ArrayList, too.

If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but still keep _someList protected.

<snip>
So, implement a SyncLock Me everytime you access your array, and
you're on the safe side.
However, if you have more then one element to synchonize which are supposed
to not exclude their manipulating code from being executed at the same

time,
then you'd choose another object for syncronisation (a string, for

example,
with a class wide scope that isn't empty and NEVER altered while the

threads
are running).


I'm a bit confused on this part. Could you elaborate?

Say I have the following code:

Public Class DataClass
Private _someList As ArrayList

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub

Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub

Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub

End Class

If I have an instance of DataClass, foo, and I have 3 threads:

Thread #1: foo.AddToList()

Would have to wait - it's SyncLocked with Me.
Thread #2: foo.EmptyList()

Would have to wait - it's SyncLocked with Me.
Thread #3: foo.DoNothingToList()

Would not have to wait. It's using the object which is used for

SyncLocking in the other methods alright, but it's actually not embedded in a SyncLock
code block.
That's actually the strange thing about SyncLock and the object it's using
as a parameter. SyncLock *does not lock the object*. It's locking *the code* and using the object only as a kind of tool to do its job - keep that in
mind!

If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until
it has finished? Does AddToList() block DoNothingToList()?

That is the part I am confused about.

Also, is it acceptable to use a SyncLock block in a method, then release

the
lock, do some processing, acquire a lock again, do something with the

locked
code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:

Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock

' Do some other stuff...

SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub

Is this ok?

Yes, that's OK.


BTW: Just in case: Keep in mind, that you musn't access controls on a form
from a thread in a direct way (by changing their properties directly,

e.g.). Only use the "diversion" over control.invoke (see vs.help on that topic for more info).

Thanks for all your help!

You're welcome!

--
Mike Loll
mi*********@hotmail.com


Nov 20 '05 #13

"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:Oq**************@tk2msftngp13.phx.gbl...
Thanks for your reply. Assuming Thread #1 in my example always executed
first, would the outcome be as follows:

Thread #1 executes and releases SyncLock.
Thread #2 acquires synclock, executes, and releases SyncLock.
Thread #3 executes whenever it can, since it is not using SyncLock.


Yes.

Nov 20 '05 #14

"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:Oq**************@tk2msftngp13.phx.gbl...
Thanks for your reply. Assuming Thread #1 in my example always executed
first, would the outcome be as follows:

Thread #1 executes and releases SyncLock.
Thread #2 acquires synclock, executes, and releases SyncLock.
Thread #3 executes whenever it can, since it is not using SyncLock.


Yes.

Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ste | last post: by
10 posts views Thread by Eric | last post: by
8 posts views Thread by MattB | last post: by
reply views Thread by Mike | last post: by
9 posts views Thread by AdrianJMartin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.