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

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 2532
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ste | last post by:
this code not work: it is no possible Exit from Monitor Why ? thank ste //////// using System; using System.Collections;
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
8
by: MattB | last post by:
Hello I am starting a new thread in a button click event. This thread calls an method which sends emails, I don't want the page to wait for the emails to finish going out as it slows the user...
0
by: Mike | last post by:
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...
9
by: AdrianJMartin | last post by:
Hi all, I have a need for a STA thread from asp.net. I can create the thread and it runs fine. But when it is finished, the thread still 'hangs' arround. Visible only to the debugger..... I get...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.