[英]Extend memory allocation, fail if not possible
I'm writing a parallel program in which I must reallocate a memory block which is actively being written to by other threads.我正在编写一个并行程序,其中我必须重新分配一个 memory 块,该块正在被其他线程主动写入。 The natural way to do this is by using realloc
, however when calling realloc
on a block which is being written to in parallel, there are two possible outcomes:执行此操作的自然方法是使用realloc
,但是在并行写入的块上调用realloc
时,有两种可能的结果:
realloc
is able to expand the allocation. realloc
能够扩展分配。 No problems occur.不会出现问题。realloc
has to create a new allocation, copy the contents of the old allocation to the new allocation, and free the original allocation. realloc
必须创建一个新分配,将旧分配的内容复制到新分配,并释放原始分配。The second case is problematic for two reasons:第二种情况是有问题的,原因有两个:
First, because a byte in the initial allocation can be written after that byte has been copied and before realloc
returns.首先,因为初始分配中的一个字节可以在该字节被复制之后和realloc
返回之前写入。 In that case, the write will be lost once the old allocation is freed.在这种情况下,一旦旧的分配被释放,写入将丢失。
Second, because realloc
will free the old allocation before it returns, and so other threads which are concurrently accessing that memory region will be reading freed memory, which is not guaranteed to be safe.其次,因为realloc
会在它返回之前释放旧的分配,所以同时访问 memory 区域的其他线程将读取释放的 memory,这不能保证是安全的。
Currently, I use a workaround, which is to malloc
, memcpy
, and then free
and use a semaphore to ensure that all threads have moved over to the new allocation before calling free
on it.目前,我使用一种解决方法,即malloc
, memcpy
,然后free
并使用信号量确保所有线程在调用free
之前都已移至新分配。
However, this approach discards the main advantage of realloc -- the possibility to extend the allocation if there is space to do so.然而,这种方法放弃了 realloc 的主要优点——如果有空间可以扩展分配的可能性。 So, my question is if there is some way to extend_allocation
that will either extend the allocation or fail (in which case I can fall back to malloc/memcpy/free), or if I have to use malloc/memcpy/free every time?所以,我的问题是是否有extend_allocation
方法可以扩展分配或失败(在这种情况下我可以回退到 malloc/memcpy/free),或者我是否必须每次都使用 malloc/memcpy/free?
Recommendations for alternative memory allocators that have solutions to this issue are also welcome as answers.也欢迎对具有此问题的解决方案的替代 memory 分配器的建议作为答案。
If you really want to use realloc
, you need to serialize access to the pointer pointing to the allocated memory before potentially modifying the pointer.如果您真的想使用realloc
,您需要在可能修改指针之前序列化对指向已分配 memory 的指针的访问。
The memory pointer should be global so any thread can reach it, along with an associated mutex or semaphore. memory 指针应该是全局的,因此任何线程都可以访问它,以及关联的互斥锁或信号量。 Anytime a thread wants to use the pointer to read or write values it first needs to lock the mutex, then unlock it when it's done.每当一个线程想要使用指针来读取或写入值时,它首先需要锁定互斥锁,然后在完成后解锁它。
Once you have this is place, you can easily grow the memory by locking the mutex, calling realloc
, then unlocking it.一旦你有了这个地方,你可以通过锁定互斥锁,调用realloc
然后解锁它来轻松地增长 memory。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.