Thread and array question

F

fniles

I am using VB.Net 2005. My main program calls 2 thread, say thread A and B.
I would like to use an array (or arraylist) that will be shared and modified
by all the main program, thread A and B.
How can I do this safely ? If I made the array (or arraylist) global, can I
modify it safely in the main program, thread A and thread B ?
Thank you.
 
A

Armin Zingler

fniles said:
I am using VB.Net 2005. My main program calls 2 thread, say thread A
and B. I would like to use an array (or arraylist) that will be
shared and modified by all the main program, thread A and B.
How can I do this safely ? If I made the array (or arraylist)
global, can I modify it safely in the main program, thread A and
thread B ?
Thank you.

You could get a synchronized wrapper by calling the shared method
ArrayList.Synchronized. The returned object is thread safe.


Armin
 
P

Phill W.

fniles said:
I am using VB.Net 2005.
I would like to use an array (or arraylist) that will be shared and modified
by all the main program, thread A and B.
How can I do this safely ?

You need to look at synchronisation mechanisms.
If I made the array (or arraylist) global,

You need something that is accessible to the three threads (main, A &
B). IMHO, having anything Global is rarely is good choice.
can I modify it safely in the main program, thread A and thread B ?

Put the array
  • inside a class and add methods/properties for
    accessing it. Use the Monitor class in /each/ of these to ensure that
    only one thread can be executing that method/property at a time.

    You can also use the Synclock keyword but I /think/ that just wraps up
    the Monitor class anyway.

    HTH,
    Phill W.
 
F

fniles

Did you mean to create the class in the main program like the following:
public class frmMainForm
dim clsArray = new ArrayClass
:
end Class

public class ArrayClass
private myArray as new arraylist
private myArraySync as arraylist = arraylist.synchronized(myArray)

sub AddArray(byval sValue as string)
monitor.enter(myArraySync)
myArraySync.add(sValue)
monitor.exit(myArraySync)
end sub
end Class

Then, how can I access the ArrayClass and call the AddArray method of
ArrayClass in thread A and B ?
Do I just pass along this clsArray to thread A and B like the following ?
Session = New ClientSession
WorkerThread = New Threading.Thread(AddressOf Session.ThreadMain)
Session.clsArray = clsArray

Public Class ClientSession
Public clsArray as ArrayList

Thank you.

Phill W. said:
fniles said:
I am using VB.Net 2005. I would like to use an array (or arraylist) that
will be shared and modified by all the main program, thread A and B.
How can I do this safely ?

You need to look at synchronisation mechanisms.
If I made the array (or arraylist) global,

You need something that is accessible to the three threads (main, A & B).
IMHO, having anything Global is rarely is good choice.
can I modify it safely in the main program, thread A and thread B ?

Put the array
  • inside a class and add methods/properties for
    accessing it. Use the Monitor class in /each/ of these to ensure that
    only one thread can be executing that method/property at a time.

    You can also use the Synclock keyword but I /think/ that just wraps up the
    Monitor class anyway.

    HTH,
    Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top