How to write a thread-safe and efficient, lock-free memory allocator in C?

前端 未结 3 927
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 12:30

How to write a thread-safe and efficient, lock-free memory allocator in C? By efficient I mean:

  1. Fast allocation & deallocation

  2. Optimal memor

3条回答
  •  旧时难觅i
    2021-01-31 13:31

    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.

提交回复
热议问题