473,786 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

good article on threads... I have a question

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.programmin g.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(cons t 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.AcquireMute x();
}
~StrictLock() {
obj_.ReleaseMut ex();
}
};
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 misunderstandin g 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
Jul 22 '05 #1
2 1468
grahamo wrote:
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.programmin g.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(cons t 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.AcquireMute x();
}
~StrictLock() {
obj_.ReleaseMut ex();
}
};
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 misunderstandin g is a C++ one on my part. If anybody can enlighten
me I'd be very grateful.


Since 'operator new' is overloaded for 'Lock' and made private, you can't
use 'new Lock' because it will attempt to call 'Lock::operator new', which
is inaccessible. However, if a Lock is a subobject of another object,
then that object's 'operator new' is used to allocate memory (or the
global one, if no overloaded 'new' exists) and the fact that 'Lock's 'new'
is private doesn't matter any more.

V
Jul 22 '05 #2
grahamo wrote:
template <class T> class StrictLock {
T& obj_;
StrictLock(); // disable default constructor
StrictLock(cons t 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.AcquireMute x();
}
~StrictLock() {
obj_.ReleaseMut ex();
}
};
The above declaration makes many things private but still has a
public constructor (actually, it would not be necessary to make
the default constructor private as generation of this constructor
is inhibited by the presence of another constructor).
"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?


Well, you can obviously make the 'Lock' class a member of 'Wrapper'.
For the construction of a 'Wrapper' object you would need to
initialize this member properly which is easily done like this:

Wrapper::Wrappe r(): memberLock(some Object) {}

Nothing prevent construction of such an object on the heap which
effectively causes the lock to reside on the heap - although nested
within another object.

However, I wouldn't consider this to be a problem: the mechanisms
in C++ prevent accidental misuses but not deliberate misuses.
Somebody put it concisely like this: "C++ defends against Murphy,
not against Machiavelli".
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3287
by: Fernando Rodriguez | last post by:
Hi, Any recommendation for a good book on multithreaded programming with Python? O:-) TIA
3
2379
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached response). My first reply directed me to source code migration but I didn't have the source code. The second reply mentioned about .NET interoperability (PInvoke I think) but I MENTIONED THAT I COULDN'T FIND ANY DOCUMENTATION FROM MSDN LIBRARY BASED ON...
10
1682
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
4
1707
by: GS | last post by:
Hi, I'd rather start from a good design and go from there so would be greatfull for any input. I have a simple ASP.NET application and would like to make solution elegant. I store settings in web.config file and I would like to have class in my application which will hold application level objects. Some methods of the class will be executed in different threads and some properties are shared among threads and static in nature. So what...
16
1962
by: Bruce Wood | last post by:
Maybe it's just late in my day, but I'm reading Jon's article on threading, in particular how to use Monitor.Wait() and Monitor.Pulse(), and there's something that's not sinking in. The code in question looks like this: public class ProducerConsumer { readonly object listLock = new object(); Queue queue = new Queue();
3
1586
by: Brian | last post by:
An MSKB article on the scalability of ADO/ASP (http://support.microsoft.com/kb/176056/EN-US/) says in a discussion of why connection objects shouldn't be stored in session variables, "If you do not pool there will be idle connections wasting server and network resources. You also have some threading issues that can occur if multiple concurrent threads end up hitting on the same connection (though the session ID might save you here, but it...
8
6213
by: Matt England | last post by:
My team currently using Boost Threads, but we are considering switching to ZThreads. (We seek cross-platform, C++ multithreading capabilities in an external library.) ZThread(s): http://zthread.sourceforge.net/ http://www.inf.uni-konstanz.de/dbis/members/vinnik/zsim/doc/ Can anyone share their ZThreads experience, either good, bad, or
244
9628
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
56
3021
by: ashore | last post by:
Guys, I see a fair bit of negativity around re subject package. Can someone share your views, either way? Thanks, AS
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.