Multithread Synchronization.

  • Thread starter Thread starter Robin Tucker
  • Start date Start date
R

Robin Tucker

I have a class called "CacheManager" that stores both (1) requests to fetch
images into the cache and (2) fetched and cached images.

My main process controls requests to fetch items into the cache (a stack)
and a worker thread services those requests, fetching the images from the
database and storing them in the cache (a hashtable).

Given that both these processes access the same instance (obviously) and
manipulate both the stack and the cache, through different methods, how can
I ensure that the two are synchronized correctly? At the moment, requests
to clear the cache (empty it, for example, when the user changes folder)
result in the worker thread failing to update the cache when it completes
(because the collection is now empty).

What I would like to do is synchronise an entire class, such that no method
on the class can be executed while any other method on that class is
currently executing.

Is this possible?

Thanks



Robin
 
Robin Tucker said:
I have a class called "CacheManager" that stores both (1) requests to
fetch images into the cache and (2) fetched and cached images.

My main process controls requests to fetch items into the cache (a stack)
and a worker thread services those requests, fetching the images from the
database and storing them in the cache (a hashtable).

Given that both these processes access the same instance (obviously) and
manipulate both the stack and the cache, through different methods, how
can I ensure that the two are synchronized correctly? At the moment,
requests to clear the cache (empty it, for example, when the user changes
folder) result in the worker thread failing to update the cache when it
completes (because the collection is now empty).

What I would like to do is synchronise an entire class, such that no
method on the class can be executed while any other method on that class
is currently executing.

Is this possible?

Yes. This should get you started. The idea is to use a seperate lock
object and lock it before touching either collection.


Class Cache
Private requests As New Collections.Stack
Private items As New Collections.Hashtable
Public ReadOnly SyncRoot As New Object

Public Sub New()
'add initialization code
'here
End Sub

Public Sub AddItem(ByVal key As Object, ByVal value As Object)
SyncLock SyncRoot
items.Add(key, value)
End SyncLock
End Sub
Public Sub AddRequest(ByVal request As Object)
SyncLock SyncRoot
requests.Push(request)
End SyncLock
End Sub
Public Function NextRequest() As Object
SyncLock SyncRoot
Return requests.Pop
End SyncLock
End Function
Public Sub ClearCache()
SyncLock SyncRoot
items.Clear()
requests.Clear()
End SyncLock
End Sub
End Class
 

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

Back
Top