我有许多读取缓存数据的读取器线程,例如
struct {
hash_table *cache;
int64_t version;
} ctx;
许多读取器线程读取缓存,例如
void *get_from_cache(void *key)
{
/* Maybe access invalid address when writer thread free it too quickly ! */
/* I can use setjmp/longjmp to deal the exception, but it's too expensive ! */
/* On Windows, I can use SEH, but how about Linux ? */
/* Finally, can I avoid it with zero cost on reader thread ? */
return cache.get(key);
}
只有一个写入器线程更新缓存:
void update_cache()
{
int64_t new_version = get_current_version();
if (new_version > ctx.version) {
hash_table *new_cache = load_cache();
hash_table *old_cache = ctx.cache;
ctx.version = new_version;
ctx.cache = new_cache;
/* How to determine the wait time is enough ? */
/* Just use a experiential value ? */
wait_some_time();
free_hash_table(old_cache);
}
}
感谢您的帮助。