I'm not using synclock correctly...

J

j3ko

Hi, I'm trying to start a thread that constantly iterates through an
arraylist of items that the main thread adds and removes from...how
would I accomplish this?

Here's the gist of what I have: (can anybody give me pointers on what
i'm doing wrong?)

Class SecondThread
aList as new arraylist
innerthread as new thread(addressof task)


public sub task()
while true
synclock aList.syncroot
for i as integer = 0 to aList.count-1
' do something
end for
end synclock

innerthread.sleep(10000)
end while
end sub

public sub start()
innerthread.start()
end sub

public sub add(item)
synclock aList.syncroot
aList.add(item)
end synclock
end sub

public sub remove(item)
synclock aList.syncroot
aList.remove(item)
end synclock
end sub
end class


my main thread calls start when I load and calls add and remove
periodically...It seems to add just fine...but a call to remove does
nothing :\
 
?

=?iso-8859-1?Q?Jos=E9_Manuel_Ag=FCero?=

Hello j3ko,

You should use a synchronized arraylist:
Dim aList as ArrayList = ArrayList.Synchronized(New ArrayList)

Using this, you don't have to synchronize read and write operations yourself:
public sub remove(item)
aList.remove(item)
end sub

You still need to synchronize enumeration or iterations through the array:
synclock aList.syncroot
for i as integer = 0 to aList.count-1
' do something
next
end synclock

Anyway, as you wrap the methods for the collection, you can make your background thread responsive using an AutoResetEvent. Your code would look like this:

Class SecondThread
Dim aList as ArrayList = ArrayList.Synchronized(New ArrayList)
Dim innerthread as new thread(addressof task)
Dim sync as new AutoResetEvent(False)

public sub task()
Do

sync.WaitOne 'Stop until sync.Set() is called anywhere.
synclock aList.syncroot
for i as integer = 0 to aList.count-1
' do something
Next
end synclock
Loop
end sub

public sub start()
innerthread.IsBackground = True 'So it ends when main thread ends.
innerthread.start()
end sub

public sub add(item)
aList.add(item)
sync.Set 'Let the background thread make a loop.
end sub

public sub remove(item)
aList.remove(item)
sync.Set
end sub
end class


Regards.


"j3ko" <[email protected]> escribió en el mensaje | Hi, I'm trying to start a thread that constantly iterates through an
| arraylist of items that the main thread adds and removes from...how
| would I accomplish this?
|
| Here's the gist of what I have: (can anybody give me pointers on what
| i'm doing wrong?)
|
| Class SecondThread
| aList as new arraylist
| innerthread as new thread(addressof task)
|
|
| public sub task()
| while true
| synclock aList.syncroot
| for i as integer = 0 to aList.count-1
| ' do something
| end for
| end synclock
|
| innerthread.sleep(10000)
| end while
| end sub
|
| public sub start()
| innerthread.start()
| end sub
|
| public sub add(item)
| synclock aList.syncroot
| aList.add(item)
| end synclock
| end sub
|
| public sub remove(item)
| synclock aList.syncroot
| aList.remove(item)
| end synclock
| end sub
| end class
|
|
| my main thread calls start when I load and calls add and remove
| periodically...It seems to add just fine...but a call to remove does
| nothing :\
 

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