-3

キャッシュデータを読み取る多くのリーダースレッドがあります。

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);
}

1 つの書き込みスレッドのみがキャッシュを更新します。

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);
    }
}

助けてくれてありがとう。

4

1 に答える 1

0

タイトルから、ポインター操作のアトミックな性質に依存しようとしているようです。ご覧くださいIs changes a pointer CAD は C のアトミック アクションと見なされますか? .

また、無効なメモリアクセスを検出しようとするのは本当に悪いことです。有効であるが正しくないアドレスへのポインターを取得した場合はどうなるでしょうか。

私は、より従来の同期メカニズムを使用します。

于 2013-01-06T14:13:18.717 に答える