I'm using a memory pool to store image data in a ray tracer and I'm using a least-recently-used algorithm to deal with freeing blocks of memory. This works fine when there is only one thread. When I add more threads, the LRU code breaks. I'm using a linked list to store the history of which blocks have been accessed and this is the part that breaks because as blocks are accessed, I have to modify pointers in the linked list which is causing conflicts between threads. This is the only way I can think to implement memory pool so I'm not really sure how to get this to work.
This is the code I'm using to bring the accessed block to the front of the recently used list where memory read errors are occurring:
LinkedListNode<int> *curTextureIndex = lastUsedTexture;
LinkedListNode<int> *prevTextureIndex = NULL;
//Find the requested texture in the recently used list
while(curTextureIndex->data != textureBlockIndex)
{
prevTextureIndex = curTextureIndex;
curTextureIndex = curTextureIndex->next;
}
if(curTextureIndex != lastUsedTexture)
{
//Bring this block to the front of the accessed list
if(prevTextureIndex != NULL)
prevTextureIndex->next = curTextureIndex->next;
curTextureIndex->next = lastUsedTexture;
lastUsedTexture = curTextureIndex;
//Set the tail of the list if necessary
if(lastUsedTexture_tail == curTextureIndex && prevTextureIndex != NULL)
lastUsedTexture_tail = prevTextureIndex;
}
Is there a good way to implement this so that it works well with multiple threads?