Do note that you've changed the pointer main_memory such that it's pointing somewhere else. When you get to deleteing the memory, you need to provide a pointer with the same value as the pointer you were given from new.
For example,
1 2 3 4 5 6 7 8 9 10
int main()
{
double* main_memory = newdouble [10000];
double* const deleteMe = main_memory; // Now safe to change main_memory becasue we keep track of the value with deleteMe
double* x1 = main_memory;
main_memory+= 7;
double* x2 = main_memory;
main_memory+= 7 ;
delete[] deleteMe;
}
I know that the memory allocation using new must be done in serial. Therefore I am not getting a good performance out of the multi-threaded program.
I've never heard of this. Are you certain memory allocation is what's consuming most of your time? Have you run the program through a profiler to find its hot spots?
I've never heard of this. Are you certain memory allocation is what's consuming most of your time?
The heap uses sets of locks to control the memory allocation. Therefore threads have to wait for the lock to release in order to allocate a memory. The following url discuss this issue and gives a solution on windows intel compilers.
I'm aware of that, but the way you said it seemed to imply that management had to be done in serial at the application level. I.e. that no more than one thread could safely call malloc() at the same time.
What you're suggesting in the OP isn't necessarily any faster than what malloc() does, since you'll still need to synchronize to move your pointer. Rather than this, would it be possible to reorder your allocations so you only need to allocate memory at the very beginning of the thread?
I ask again, though: are you certain this is what's consuming most of your time?