ConcurrentMap 并发映射的使用

2019-03-06  本文已影响0人  暮阳晨鼓
  1. 源码:https://github.com/preshing/junction

使用步骤

  1. 生成libjunction.a和libturf.a
git clone https://github.com/preshing/junction.git
git clone https://github.com/preshing/turf.git
cd junction
makdir build
cd build
cmake ..
make
  1. 使用静态库

ConcurrentMap

1. 为什么使用ConcurrentMap

2. ConcurrentMap库

image.png

3. concurrentMap的使用

#include " ConcurrentMap_Grampa.h"
typedef junction::ConcurrentMap_Grampa<int, Student*> ConcurrentMap;
ConcurrentMap myMap;
void testMap{
  myMap.assign(taskId, new Student) // 将id和空间对应
  myMap.get(taskId); // 获取对应的空间
  myMap.erase(taskId); //在map中释放这个空间
  myMap.exchange(taskId, Student* student) //将key的value值换一个
}
总的来说,map它不会分配空间,调用assign函数时,空间都是我们分配好了,然后把指针传过去。因此除了
erase外,我们还需free或者delete相关内存
int main{
// Create QSBR context for the main thread.
    junction::QSBR::Context context = junction::DefaultQSBR.createContext();
 // Run a simple map test.
    testMap();

    // Update the QSBR context for this thread.
    // In a larger application, this should be called periodically, for each thread, at a moment
    // when the thread is quiescent – that is, not in the middle of any operation that uses a
    // Junction data structure.
    junction::DefaultQSBR.update(context);

    // Destroy the QSBR context for the main thread.
    junction::DefaultQSBR.destroyContext(context);
return 0
}

小结

上一篇下一篇

猜你喜欢

热点阅读