473,396 Members | 1,975 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.

Multithread Synchronization.


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
Nov 21 '05 #1
2 1155
Robin,

Do you know that there is a special MSDN page about your question.

http://msdn.microsoft.com/library/de...ronization.asp

I did not do this myself yet,

Cor

"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

Nov 21 '05 #2

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cm*******************@news.demon.co.uk...

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


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alice | last post by:
Hello I have four multithread windows applications(vb.net) interacting and running on the same machine(windows 2000 with .net framework 1.0). All of them start a new thread each time Filewatcher's...
0
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
2
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
0
by: fred | last post by:
I need some help in trying to understand how to make myCollection (inherited from CollectionBase) multithread safe. Taking my implementation of the Add Sub and a readonly property Item. Public...
6
by: jmartin | last post by:
Hi, I have made a multithread version of a program (load a file into database), and with two processors I get the double of time in the multithread than in the process (unithread) version. I...
12
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.