如何用C语言编写一个线程安全、高效、无锁的内存分配器?
如何用C语言编写一个线程安全、高效、无锁的内存分配器?我所说的高效是指:
快速分配和快速分配。解除分配
最佳内存使用(最小浪费,无外部碎片)
最小元数据开销
How to write a thread-safe and efficient, lock-free memory allocator in C? By efficient I mean:
Fast allocation & deallocation
Optimal memory usage (minimal wastage and no external fragmentation)
Minimal meta-data overhead
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://www.research.ibm.com/people/ m/michael/pldi-2004.pdf
http://www.research.ibm.com/people/m/michael/pldi-2004.pdf
取决于你所说的效率是什么意思。如果我关心的是让事情变得更快,那么我可能会给每个线程它自己的单独的内存池来使用,以及一个从该池中获取内存的自定义“malloc”。当然,如果我关心的是速度,我可能会首先避免分配。
没有一个答案;您将平衡一系列问题。获得无锁分配器几乎是不可能的,但是您可以尽早且不频繁地进行锁定(通过为每个线程分配大池),或者您可以使锁变得如此小和紧密以至于它们必须是正确的。
Depends on what you mean by efficiency. If my concern was to make things fast, then I would probably give each thread it's own separate memory pool to work with, and a custom 'malloc' that took memory from that pool. Of course, if my concern was speed, I would probably avoid allocation in the first place.
There is no one answer; you'll be balancing a range of concerns. It will be pretty much impossible to get a lock-free allocator, but you can either do the locking early and infrequently ( by allocating large pools for each thread ) or you can make the locks so small and tight that they must be correct.
您可以使用无锁列表和几个不同大小的存储桶。
所以:
SLIST_ENTRY、InterlockedPushEntrySList、InterlockedPopEntrySList、InitializeSListHead
是Win32下无锁单链表操作的函数。其他操作系统请参考相应操作。缺点:
sizeof( SLIST_ENTRY )
的开销You can use a lock free list and a couple of buckets of differing sizes.
So :
SLIST_ENTRY, InterlockedPushEntrySList, InterlockedPopEntrySList, InitializeSListHead
are functions for lock-free single-linked-list operations under Win32. Use the according operations on other OSes.Drawbacks :
sizeof( SLIST_ENTRY )