如何使用共享内存实现哈希表 [英] How to implement hash table using shared memory

查看:437
本文介绍了如何使用共享内存实现哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写一个复杂的结构,该结构在共享内存中包含许多指针.
是否在共享内存中使用哈希表实现?

在此先感谢

Hi ,

I am trying to write a complex structure which contains many pointers in shared memory .
Do I use hash table implementation in shared memory ?

Thanks in advance

推荐答案

代替指针使用偏移量(例如,相对于共享内存的开头),因为共享内存可能会在不同的进程中映射到不同的偏移量.具有单独链接的哈希表实现由一个哈希数组和一个items数组(通常在items数组中没有孔)组成.哈希表条目包含该项目的哈希值和具有指定哈希值的表中第一个项目的索引,在items表中,您还通过索引链接了具有相同哈希值的项目.因此,如果正确实现哈希表就不是问题,您应该小心放置在其中的数据,数据应该是自包含"的,这样它就不会在哈希表之外存储数据,否则您的数据必须使用分配器来限制分配,该分配器驻留在共享内存中.这个分配器变得难以管理,因此请使用没有指针和分配的自包含项!

如果编写哈希表实现(让我们说一个模板),则必须使用new放置来构造它,并在指定的共享内存区域上通过直接析构函数调用对其进行析构. http://www.parashift.com/c++-faq-lite/placement-new.html [^ ]

您可以在Google上找到C ++哈希表实现,只需修改指向索引的指针即可.
Instead of pointers use offsets (relative for example to the beginning of the shared memory) because the shared memory might be mapped to different offsets in different processes. A hash table implementation with separate chaining consists of a hash array, and an items array (usually without holes in the items array). The hash table entry contains a hash for the item and an index to the first item in the table with the specified hash, in the items table you also link the items that have the same hash by indices. So the hash table isn''t a problem if implemented correctly, you should be careful with the data you place in it, the data should be "self contained" so that it doesn''t store data outside the hash table otherwise your data must use an allocator that restricts allocations a pool that resides in your shared memory. This allocator thingy becomes hard-to-manage easily so go with self contained items without pointers and allocation instead!

If you write your hash table implementation (lets say a template) then you have to construct it with placement new and destruct it with direct destructor call on the specified shared memory area. http://www.parashift.com/c++-faq-lite/placement-new.html[^]

You can find C++ hash table implementations with google, all you have to modify is changing the pointers to indices.


这篇关于如何使用共享内存实现哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆