ext/hash_map Multithread

I'd like to craete multithread program which uses hash_map (hash_map<int, vecotr<void *> >). I have 8-core machine so I don't want to lock hash_map every insert. I created 20000 phtread_rwlock's and when there is insert value using some key i do pthread_rwlock_wrlock(locks[key % 20000]) and during read I create pthread_rwlock_rdlock(locks[key % 20000]). That solution segfaults sometimes.

Any ideas what should I do with that problem?
Last edited on
What are you locking?
I want to lock hash_map before insert
Something like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

hash_map<int, vecotr<void *> > myMap;

void writeValue()  //runs in many threads
{
    int *some_val = new int;
    pthread_rwlock_wrlock(locks[key % 20000]);

    myMap[key].push_back((void*)(some_val));

    pthread_rwlock_unlock(locks[key % 20000]);
}

vecotr<void *> readValue()  //runs in many threads
{
    vecotr<void *> vec;
    pthread_rwlock_rdlock(locks[key % 20000]);

    if (myMap.count(key))
       vec = myMap[key];

    pthread_rwlock_unlock(locks[key % 20000]);
    return vec;
}



Any ideas?
Last edited on
Topic archived. No new replies allowed.