G
grahamo
Hi,
I realise that c++ knows nothing about threads however my question is
related to an (excellent) article I was reading about threads and C++.
For all intents and purposes we can forget the sample at hand is
concerned about threads, it could be related to widgets for the
purpose of this question (that's so I don't get flamed and told to go
to comp.programming.threads
However while discussing how to define a template class to be used for
synchronisation, the author defines it thus;
template <class T> class StrictLock {
T& obj_;
StrictLock(); // disable default constructor
StrictLock(const StrictLock&); // disable copy constructor
StrictLock& operator=(const StrictLock&); // disable assignment
void* operator new(std::size_t); // disable heap allocation
void* operator new[](std::size_t);
void operator delete(void*);
void operator delete[](void*);
StrictLock* operator&(); // disable address taking
public:
StrictLock(T& obj) : obj_(obj) {
obj.AcquireMutex();
}
~StrictLock() {
obj_.ReleaseMutex();
}
};
He then goes on to say how these definitions prevent copy
construction, construction on the heap, etc. etc. That's fine and
dandy. However he then mentions;
"You cannot allocate a StrictLock on the heap. However, you still can
put StrictLocks on the heap if they're members of a class.
class Wrapper {
Lock memberLock_;
...
};
Wrapper* pW = new Wrapper; // ok "
How is this so? Given the declaration above and the code he cite, how
can StrictLocks be put on the heap if they're members of a class? I
trust this dude based on everything else he's said in the article so
the misunderstanding is a C++ one on my part. If anybody can enlighten
me I'd be very grateful.
Cheers
GrahamO
BTW, the threads article can be found at;
http://www.informit.com/articles/article.asp?p=25298
I realise that c++ knows nothing about threads however my question is
related to an (excellent) article I was reading about threads and C++.
For all intents and purposes we can forget the sample at hand is
concerned about threads, it could be related to widgets for the
purpose of this question (that's so I don't get flamed and told to go
to comp.programming.threads
However while discussing how to define a template class to be used for
synchronisation, the author defines it thus;
template <class T> class StrictLock {
T& obj_;
StrictLock(); // disable default constructor
StrictLock(const StrictLock&); // disable copy constructor
StrictLock& operator=(const StrictLock&); // disable assignment
void* operator new(std::size_t); // disable heap allocation
void* operator new[](std::size_t);
void operator delete(void*);
void operator delete[](void*);
StrictLock* operator&(); // disable address taking
public:
StrictLock(T& obj) : obj_(obj) {
obj.AcquireMutex();
}
~StrictLock() {
obj_.ReleaseMutex();
}
};
He then goes on to say how these definitions prevent copy
construction, construction on the heap, etc. etc. That's fine and
dandy. However he then mentions;
"You cannot allocate a StrictLock on the heap. However, you still can
put StrictLocks on the heap if they're members of a class.
class Wrapper {
Lock memberLock_;
...
};
Wrapper* pW = new Wrapper; // ok "
How is this so? Given the declaration above and the code he cite, how
can StrictLocks be put on the heap if they're members of a class? I
trust this dude based on everything else he's said in the article so
the misunderstanding is a C++ one on my part. If anybody can enlighten
me I'd be very grateful.
Cheers
GrahamO
BTW, the threads article can be found at;
http://www.informit.com/articles/article.asp?p=25298