jeudi 4 mars 2021

Is it thread-safe to allocate a memory with new? [duplicate]

If I have some function that create new objects and return it's id, ignoring for now the moment how I will store this pointer

int foo()
{
   SomeClass *p = new SomeClass();
   return p->id();
}

Imagine only this meaningless code snippet, will it be tread-safe? If two threads come almost simultaneously into the function, there is no way that they will try to allocate memory in the same(intersected) place? And calling method by pointer should be also thread-safe, as we are just reading the memory.

Aucun commentaire:

Enregistrer un commentaire