Memory – multithreaded heap management

In C / C + +, I can allocate memory in one thread and delete it in another thread However, as long as someone requests memory from the heap, the heap allocator needs to go to the heap and find a free area of appropriate size How can two threads effectively access the same heap without breaking the heap? (is this done by locking the heap?)

Solution

Generally speaking, you don't need to worry about the thread safety of the memory allocator All standard memory allocators - that is, memory allocators attached to MacOS, windows, Linux, etc. - are thread safe Locks are a standard way to provide thread safety, although you can write memory allocators that use only atomic operations rather than locks

Now this is a completely different question: whether these memory allocators scale; That is, their performance is independent of the number of threads performing memory operations? In most cases, the answer is no; They either slow down or consume more memory The first extensible allocator (speed and space) is hoard (I wrote it); The Mac OS X allocator was inspired by it – and referenced in the documentation – but hoard is faster There are others, including Google's tcmalloc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>