Hi all,
I am not sure how to explain exactly what I am hoping to do here, but I will do my best in the hopes that someone will understand.
I am writing a program that uses a cache, this cache is used with a thread that is seperate from the main program, this works perfectly, the cache is a seperate class.
Now the problem, the cache gets updated hourly from an external file, basically the method is that every minute or so the program checks the stat on the file and if the atime has altered it rebuilds the cache, now I need my thread that is using the cache to use totally transparently the new cache.
Since the building of the new cache every hour is handled by a seperate thread I can't figure out a sane method of updating the class in use by the first thread.
I have tried writing a seperate class called cache_control that works basically to build references ie:
class cache_control {
public:
Cache private_cache;
Cache public_cache;
void reinit_cache(void);
}
void cache_control::reinit_cache(void) {
private_cache.read_input_file();
Cache & pubic_cache = private_cache;
}
Please excuse the roughness of this code it is just an example of the type of thing I am attempting to do.
As you see I am making 2 caches the private and the public the public_cache is just a reference to the private_cache object and when my thread is started it is passed a reference to the main cache_control object from main() and it uses the public_cache it access its methods and data, that way when I need to rebuild the cache I just reinit_cache the private_cache and it then points public_cache to the new instance of private_cache.
I hope you are with me so far.
The problem I have here is that firstly I am creating the object Cache public_cache before I get the chance to declare it as a reference and I don't think this is allowed (I may be wrong) and secondly, I think that perhaps having a reference to a class that references a class that isn't inherited, is perhaps a dangerous thing to do, and thirdly it just plain doesn't seem to work.
I am basically looking for a good suggestion from someone on a better way of doing this, I though perhaps I should make my Cache class in inherited class from cache_control but the way I see it is that will just make it a little cleaner it won't actually make it work, I also considered creating a global vector something like:
vector<cache> global_cache;
Then when I need to rebuild the cache I simply build a new instance of the cache delete the instance currently in the vector and add the new one, but I think perhaps that will create some more problems in itself not to mention the fact that my test programs using a vector with type <cache> all seem to die at run time no matter that I do with them.
What I am hoping to avoid is having to kill the thread and restart it with a new copy of the data is the program itself is a fairly high demand searchengine and by killing the thread I would have to kill all client connections.
Any suggestions would be very welcome I have been pulling my hair out on this one for a couple of days, if anymore explaination of what I am hoping to do is needed please let me know.
Thanks
Ben