Hi there all --

I am planning to create multipal threads for execution of specific functions with in my app, but i need to save the HANDLE's of each so i have decided to use heap functions as even 1MB will be 262144 handles inside the allocated memory. So what i would like to know, is when you realloc the heap... is the values contained before the reallocation still valid. IE does the operation preserve the contents of the previous allocation?

Black iCE
Posted on 2004-09-06 01:57:09 by Black iCE
From MSDN (by searching for HeapReAlloc):

"HeapReAlloc is guaranteed to preserve the content of the memory being reallocated, even if the new memory is allocated at a different location."

It seems to me that there would be no point to HeapReAlloc if it didn't preserve the contents of the memory, because otherwise you could just make a simple function that frees the memory and then allocates new memory.
Posted on 2004-09-06 02:20:22 by yessopotamus
OK, i was thinking about that while typing the question -- but then i still asked didn't i? Oh well neva hurts to be certain. Shot, thanks.

Black iCE
Posted on 2004-09-06 02:23:13 by Black iCE
Since i was talking about multi-threading here --
So when i create the thread i save the handle into the allocated memory, but what do you all here think can i do to know which thread has ended... as they might end in any order and so the question about reusing the approiate 4 bytes in the allocated memory comes to the fore. The thread upon creation can optionally have an ID but that is worthless as in the thread itself i don't know how to relate it to the appropiate HANDLE.


ALLOCATED MEMORY:[handle1][handle2][handle3]....

Thread 1: Exit with NULL upon success
Thread 2: busy
Thread 3: Exit with NULL upon success

So from thread 1 & 3, how will i know where to clear the memory?
Posted on 2004-09-06 03:01:07 by Black iCE
If all you want to do is invalidate the handle in the list, all you have to do is tell the thread the index into the list and have it invalidate that entry on exit.

But as for finding an empty place on the list to use for the next thread that is created, the simplest (but by far the slowest) way is to look up the list from the beginning until you find an empty slot.

If you don't care at all about finding the lowest empty slot, what you can do is make a "stack"-like data structure on which you push empty slot IDs when a thread ends, and from which you pop empty slot IDs when you create a thread. (If there is nothing to pop, increase the size of the list and use one of the newly created IDs.)

If you do need to find the lowest empty slot, what you can do is make a binary tree, consisting of one root node, a bunch of child-less nodes (which represent actual slots), and a bunch of other nodes to fill in the binary tree. Each node that has children knows whether or not there is a free slot somewhere if you traverse down either child. When a slot is filled or emptied, travel up the tree and update the ancestor nodes so that this information always stays correct.

In either case you should realize that this is a problem of "slots" and not a problem of "threads", so your solution should be programmed into a separate module that solves the problem no matter what problem you have to apply "slots" to.
Posted on 2004-09-06 03:21:08 by yessopotamus