Reputation: 255
I have the following class:
struct globalAllocated
{
void operator delete(void*p)
{
static HANDLE heap= GetHeap();
::HeapFree(heap, 0, p);
}
}
warning C4640: 'heap' : construction of local static object is not thread-safe
I thought about using some synchronization with mutex but it seems costly.
Making the heap a private member won't work because the operator delete override must be static, so heap must be private- but if I declare it as a static class member, there is no where for me to initialize it.
What's the best solution?
Upvotes: 1
Views: 634
Reputation: 153919
If you can use C++11, it's required to be thread-safe there. (But that could easily be implemented by using a mutex as well, if you're concerned about performance issues.)
More generally: try to ensure that the function is called before multiple threads are started. (In many applications, all that is needed is to call it somewhere in the initialization of a static object.)
Upvotes: 2