Question

I have a critical section that is shared between two threads:

TCriticalSection        lock_measDataBuff;

I have declared this variable as global. Now because Delphi style classes must be constructed using operator new, i have modified above declaration as follows:

TCriticalSection        *lock_measDataBuff;

Where is the best place to initialize the lock variable using operator new? Where is the best place to finalize the global variable using operator delete? Should it be WinMain method? Constructor of one of the classes accessing lock variable? Or some other place in the code?

Était-ce utile?

La solution

I would use std::auto_ptr or boost::unique_ptr to handle all of that for you, eg:

#include <memory>

std::auto_ptr<TCriticalSection> lock_measDataBuff(new TCriticalSection);

Autres conseils

As you are creating a global variable, you will need to initialize before you create the threads, which would be main, and the best place to release the memory would be after the threads end

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top