2009 Jan 02 - Fri
Wanted: A Single C++ Singleton
The Singleton Concept is a reasonably simple concept.
For writing software, the concept of a singleton stipulates that only one instance of a class will be instantiated during run
time. All references to an object of a particular class will be to only one instance. The instance is usually created at program
startup, often times before 'main', and destroyed at programs end, often after 'main' exits.
I wish to use the concept of a Singleton in my C++ based trading software for Manager classes which keep track of Providers,
Instruments, and Portfolios.
Thinking that the Singleton Design Pattern was simple, I figured I could implement my own flavour. But I decided to do some
research first. It turns out there are simple ways, complicated ways, and controversial ways.
From the controversial side, some consider using Singletons as
less than desirable, as the concept introduces global state, which in turn reduces modularity, compartmentalization, and as a
consequence increases the complexity of testing. These are valid reasons, and I see it being applicable to situations where a
programmer uses Singletons for small objects or built-in data types.
In my situation, I wish to form Singletons of larger self-contained classes. It doesn't make logical sense to have multiple
managers, and as such, the Singleton concept enforces/implmements my need for singleton managers.
With the Boost Libraries being as
comprehensive and well written as they are, I figured they should offer up a good singleton of a Singleton implementation. Nope. In
doing a search through the library, I find about four or six or more different flavours of singleton.hpp, and nothing in the 'common
areas' of the library:
- boost/serialization/singleton.hpp: uses boost::noncopyable, has medium multi-threading capability, and contains some .dll
dependencies
- boost/log/detail/singleton.hpp: uses boost::noncopyable, is a simpler class, not sure if it is thread safe, and makes use of
#defines like BOOST_ONCE_INIT
- boost/pool/detail/singleton.hpp: a simple, self-contained class designed for instantiation before main, basically a type of
Meyer's Singleton but using a template mechanism
- boost/thread/detail/singleton.hpp: a very minimalistic singleton
There are notes in some locations indicating that these shouldn't be used as they are essentially 'library internal' routines and
are subject to non-documented changes.
hmmm, maybe Singletons are complicated.
Alex Ott's Blog mentioned
that there was actually a
Singleton submission (documentation) to Boost back in the beginning of 2008. The submission promised
to handle single threaded and multi-threaded implementations of the Singleton Pattern. It was
rejected. Reviewers wanted to see
a more modularized approach. The submitter indicated that he has/had run out of time to do so. In reading the review thread, a number
of writers didn't like the complexity of the Loki library, and thus the Boost submission took a different tact. I'm of an
impression that the library submission would have a good change of succeeding if it followed the
'programming by policy' method used in Loki. Under the hood there is some complexity, but to the user, the interface is clean and modular.
For those interested in the submission code, It resides mouldering in the
sandbox.
Loki, which was started by
Andrei Alexandrescu in his book on "Modern C++ Design", has a Singleton
implementation, but no on-line documentation. I went into his book and I see that he goes into some detail on the design ideas and
usage notes regarding his Loki SingletonHolder class. Indeed, he says that there is no single size-fits-all singleton. And when
looking at the doxygen class notes, there is a variety of construction, lifetime, and threading template traits available. Perhaps
this file could be spruced up and submitted to Boost.
Andrei and Scott Meyers wrote a paper back in September 2004 called
C++ and the Perils of Double-Checked Locking. It goes into the gory details of why multi-threaded Singletons are so hard to
implement. Much of it has to do with compiler optimizations, and the fact that C++ machine states are defined for single threaded
models only. It is interesting to note that implication that C++ is really multi-threaded, from a philosophical and design
perspecitve. It has been forced into that world with assemlby code and operating system api work arounds. C++ has been so malleable
when it comes to metaprogramming, object oriented programming, and any of a number of other programming paradigms. To fall down on
the job of multi-threading may be an indication of the difficulties inherent in moving from single-threading to
multi-threading and multi-core processing.
In addition to the library submitted to Boost, another author offers up his version of a
Thread-Safe C++ Singleton. His writing indicates he
uses the concept of a Phoenix Singleton, a Singleton which can recreate itself. The book Modern C++ Design goes into a description
of this.
For a simple, single-threaded, self contained Singleton which manages itself, a
C++ Singleton Pattern is available. It uses the
Curiously Recurring Template Pattern
(CRTP). It uses an override of new and delete but does not use reference counting to keep things straight, which may cause
problems in some use cases. It is like a Phoenix Singleton but doesn't really do LIFO type creation/destruction properly.
Scott Meyer's "Effective C++ Third Edition" has a description of what has been termed Meyer's Singleton. It does all the
constructor, destructor, and assignment hiding and provides a built-in static method for returning a reference to the object
instance. Here is a specific version of
The Meyers Singleton.
class InstrumentManager {
public:
static InstrumentManager &Instance() {
static InstrumentManager _InstrumentManager; // local static object initialization
return _InstrumentManager;
}
void BasicMethod( void );
private:
InstrumentManager(); // constructor (ctor) is hidden
InstrumentManager( InstrumentManager const & ); // copy ctor is hidden
InstrumentManager &operator=( InstrumentManager const & ); // assignment operator is hidden
~InstrumentManager() // destructor (dtor) is hidden
};
A Generic Meyers Singleton:
// singleton.h
#ifndef __SINGLETON_H
#define __SINGLETON_H
template class CSingleton {
public:
static T& Instance() {
static T _instance;
return _instance;
}
protected:
private:
CSingleton(); // ctor hidden
~CSingleton(); // dtor hidden
CSingleton(CSingleton const&); // copy ctor hidden
CSingleton& operator=(CSingleton const&); // assign op hidden
};
#endif
In summary, I think I'll end up using the boost::detail::pool as it can be used to wrap general classes without resorting to
writing classes as specific Meyers Singletons, which may or may not be a good thing. If, at some point in the future, I get
some free time, tackling the Loki Singleton to Boost Singleton conversion might be an interesting learning experience.
[/Personal/SoftwareDevelopment/CPP]
permanent link
2008 Dec 28 - Sun
Modern Day Vikings
I don't have access to the paper, but the abstract looks interesting:
Looting: The Economic Underworld of Bankruptcy for Profit. Sometimes I think that some
companies do a business plan around this, or implement it through 20::20 hindsight. Back a
few years ago, companies were laying fibre like crazy. Over capacity resulted. Many went
'under' and resurfaced with the assets but less debt overhead after writing investors off.
[/Trading/ReadingMaterial]
permanent link
2008 Nov 27 - Thu
TFTP
There are a number of TFTP servers available. I had written about atftpd in a related article. Research indicates that
tftpd-hpa is another popular tftp server. This article provides a few hints on its installation. Although designed for
remote boot capability for PXE (and for handling larger files), I use it mostly for device configuration and image loading.
Basic installation on Debian is straightforward: apt-get install tftpd-hpa
I created a local sub-directory called /var/local/tftpd. Traditionally, people use /tftpboot but I wanted the files in
the traditional /var/local location instead.
I modified /etc/default/tftpd-hpa to have this line: OPTIONS="-l -c -u tftpd -s /var/local/tftpd"
I added a user and group called tftpd.
I disabled the tftp entry in /etc/inetd.conf, and restarted inetd.
Starting the service: /etc/init.d/tftpd-hpa start
I use iptables for inbound/outbound protection, so needed to add rules for the tftp protocol. For protocol inspection
the connection tracker is needed: modprobe ip_conntrack_tftp
[/OpenSource/Debian/Monitoring]
permanent link
2008 Nov 24 - Mon
TTCP: Test TCP
A quick and simple tool for link bandwidth testing is included in many flavours of
Cisco's IOS.
Although it is hidden and officially unsupported, it is documented and functional.
By running 'ttcp' from the command line in privileged mode on two different routers, one
can test links between the routers.
Cisco documents the tool with
Document 10340, Using Test TCP (TTCP) to Test Throughput.
A couple of other non-Cisco tools are available and maintain compatibility in order to
perform link testing between most combinations of routers, Unix, Linux, and Windows
platforms.
Netcordia has a
Java based client, while Unix and Windows based client can be found at
ttcp. The source compiled on
Linux with no problem.
In
The Story of the TTCP Program, Mike Muuss
discusses some of the history of ttcp. It seems that he is the original author of the venerable ping program. In
the same article, Mike illustrates a clever file transfer capability of ttcp, if effect being a UNIX "pipe"
between two machines across a network. On the destination machine:
ttcp -r | tar xvpf -
and on the source machine:
tar cf - directory | ttcp -t dest_machine
and on possible intermediate machines:
ttcp -r | ttcp -t next_machine
A discussion of additional variants of ttcp can be found at
ttcp/nttcp/nuttcp/iperf versions. It looks
as though the version used by Cisco is a renamed nttcp. A version called
nuttcp will echo traffic back.
iperf uses the same concept but uses a
different name and includes different functionality for network perforamance analysis. I'm not sure if it is
compatible with ttcp.
[/Cisco]
permanent link
2008 Nov 23 - Sun
Seasonalality Timing System
In Mark Hulbert's November 17, 2008 article called
The long-term reasserts itself,
he mentions the Seasonality Timing System (STS) designed by Norman Fosback of the Fosback's Fund Forecaster Newsletter.
The STS is designed around the fact that "the stock market has a bullish bias around the trading sessions
immediately prior to each exchange holiday as well as those at the turns of each month." He indicates that
"STS followers will not get back into the stock market until the close next Monday, so as to be fully invested for the
sessions on Tuesday and Wednesday, the two trading sessions prior to the market's Thanksgiving holiday."
According to those remarks, trading for this upcoming week should give us a rebound.
The picture turns less rosy with Paul Farrell's November 19, 2008 article
30 reasons for Great Depression 2 by 2011.
Basically he says more spending with little or no increased income is a recipe for further disasters.
Peter Brimelow in a November 20, 2008 article called
Bears' glass half empty or half full?,
writes about Dow Theorist Richard Russell indicates that the primary bear market has been reconfirmed, and things are
headed lower, perhaps to around the 7286 (the 2002 Dow Low) and 7470 (half the bull market peak), which we touched
Thursday and Friday, but were saved by the news of Timothy Geithner, now president of New York Federal Reserve, would
be Obamas's Treasury Secretary.
Corey Rosenbloom has confirmation of the
Interesting Fibonacci Development. We have a level of support at about 7500, and a trading range up to a level of
Fibonacci and psychological resistance of about 10,000.
But then more bad news could be around the corner. Lots of interesting Economic news coming this week: Existing
Home Sales on Monday, Tuesday with GDP and Consumer Confidence, and then the day before the US Thanksgiving we have
Durable Goods, Personal Income, Jobless Claims, Consumer Sentiment, and New Home Sales.
[/Trading/MarketNotes]
permanent link
2008 Nov 14 - Fri
Receding Recession Indicator
The last time the Dow was at current levels looks to be back in May of 2003. But going
back a bit more, it was May of 2002 that the Dow dropped below 10,000. It hit a low of 7600
during the beginning of October 2002. December and January were relatively 'happy' months
before the Dow retested 7700 in March 2003. It took a steady rise till December 2003 to
cross
back above the magic 10,000. The year 2004 saw a few minor dips below 10,000, but nothing
serious. October 2007 seeems to have been the recent peak at around 14,000. It declined
bit by bit until September/October of this year when it bit the dirt.
In the last few weeks, it hit a low of 8451 around Oct 10,
another lower low October 27 of about 8175, and retested with a mid-low at 8282 on
November 12.
All this to say that we haven't made any recent lower lows. Yet.
Leonard Novy says a
symmetrical triangle is forming prior to a
head and shoulders finalization at a still lower level. We shall see.
And if history offers
any pattern for the future, we could stay at this level for six to twelve months. Things
could improve over the next bit. Come next year, there are supposed to be more mortgage
resets, which may cause another economy/financial hit, more people losing homes, and as a
result
jobs. After that, hopefully people's eternal
optimism will start to kick in, and it is possible we could see a 10K Dow by the end
of 2009 or first quarter 2010.
According to
Donald Luskin,
the bear market will be over when "stocks have rallied at least 20% from any given low
point, over at least two calendar months". The pattern in December 2002 almost but not
quite made the 20% criteria. It wasn't till after March 2003 did things conform to pattern.
Perhaps 2008/2009 may hold a similar pattern to 2002/2003.
[/Trading/MarketNotes]
permanent link
2008 Nov 04 - Tue
Debian Lenney Exim Configuration
I can't recall, but I think by default, on a Debian Lenny install, the email server is
configured to send email locally only. In order to get it to send email to other servers,
the following command needs to be run to reconfigure Exim:
dpkg-reconfigure exim4-config
This is in response to a Non Delivery Report (NDR) of: Remote Domains Not Supported
[/OpenSource/Debian]
permanent link
2008 Nov 03 - Mon
Multiple Switch Interfaces
Acktomic's genDevConfig creates it's Default files with one interface per view. For
switches, it would be nice to see all interfaces presented on one page. To do this, I
manually create a file to show these interfaces on one page. Here is a sample config:
target --default--
devicename = sw35
directory-desc = ""
interface-name = ""
long-desc = %short-desc%
short-desc = ""
target-type = cisco-interface
target sw35-ports
targets = "/switches/sw35/FastEthernet0_1;
/switches/sw35/FastEthernet0_2;
/switches/sw35/FastEthernet0_3;
/switches/sw35/FastEthernet0_4;
/switches/sw35/FastEthernet0_5;
/switches/sw35/FastEthernet0_6;
/switches/sw35/FastEthernet0_7;
/switches/sw35/FastEthernet0_8;
/switches/sw35/FastEthernet0_9;
/switches/sw35/FastEthernet0_10;
/switches/sw35/FastEthernet0_11;
/switches/sw35/FastEthernet0_12;
/switches/sw35/FastEthernet0_13;
/switches/sw35/FastEthernet0_14;
/switches/sw35/FastEthernet0_15;
/switches/sw35/FastEthernet0_16;
/switches/sw35/FastEthernet0_17;
/switches/sw35/FastEthernet0_18;
/switches/sw35/FastEthernet0_19;
/switches/sw35/FastEthernet0_20;
/switches/sw35/FastEthernet0_21;
/switches/sw35/FastEthernet0_22;
/switches/sw35/FastEthernet0_23;
/switches/sw35/FastEthernet0_24;
/switches/sw35/GigabitEthernet0_1;
/switches/sw35/GigabitEthernet0_2"
short-desc = "Sw35 ports"
[/OpenSource/Debian/Monitoring/Cricket]
permanent link
Cricket Summation
I have a number of routers, each with an interface to an upstream provider. I'd like to
show a graph with the three providers aggregated. This is a config I did to do so:
target --default--
devicename = statistics
directory-desc = ""
interface-name = ""
long-desc = %short-desc%
short-desc = ""
target-type = standard-interface
target ProviderAggregate
mtargets = "/routers/router1/fastethernet2_0;
/routers/router2/serial1_0;
/routers/router3/atm2_0.1-aal5_layer"
mtargets-ops = "sum()"
short-desc "sum(Prov1, Prov2, Prov3)"
The above is the content of a file located in the /routers subdirectory. This turned out
to be easier than I thought. The file needs the target --default-- section to start. Then
one or more of the aggregate targets can be present. The 'mtargets' simply needs to know
the directory and interface. The basic Default configurations in each subdirectory were
created with devConfig tool from Acktomic.
[/OpenSource/Debian/Monitoring/Cricket]
permanent link
2008 Nov 02 - Sun
Governmental Capitalism
The article "Why The Mortgage Crisis Happened" goes into some detail regarding
the political background of the current financial situation originating in the US and
spreading through out the world.
Some might say it was capitalism running rampant. But it looks more like the government
trying to do the socialist thing and trying to get home ownership into the hands of those
who can't/couldn't afford it. Isn't that what credit reports are for? In the words of Scott Francis: "The
Community Reinvestment Act is a freaking joke. Why should a minority have a different set of rules and credit
requirements than someone who has good credit?"
It is interesting that John McCain is painted in a positve light as knowing about the
situation and attempted to do something about it. Obama, on the other hand, is painted in a
bad light as being a perpetrator of the whole situation, and even accepted money to
perpetuate the whole fiasco. And guess who it looks like the US will have as it's next
president? Unless the undecided's all vote for McCain. Which shows my bias. But, perhaps
in some version of the future, the US may field a third political strong enough to bite the
hands of both the consumer and big business and make the decisions necessary to reduce
the size of government, the debt, and everything else. Yeah, right. Too many
self-interested groups.
According the article, business institutions needed to bury the good with the bad.
However, it seems that the bad started to infect the good in a larger degree than was
thought possible. Then (over-)leverage opened the whole festering wound. Please note that my remark
regarding over-leveraged Wall Street places a good chunk of follow on blame on the rocket scientists who
attempted to help monetize the government's problem. A commenter named Terry writes that the article fails to
"fully examine the role of the conversion of mortgages into mortgage backed securities that were improperly
rated by corrupted rating agencies and then sold into the marketplace. This, in combination with the looming
problem with credit default swaps, is a much more significant pathogen in this disease process. " Terry
indicates that this is an issue of deregulation, something of which the 'conservative commentator' doesn't
cover.
According to
Foreclosure Myths: Can the Media Handle the Truth?, the media is suggesting that the crisis was started
through "Americans overwhelmed by circumstances beyond their control, from job losses to health problems to
personal crises like divorce which ultimately cost them their homes." "the foreclosure problems began in
mid-2006 when the nation.s unemployment rate was holding steady at a mere 4.6 percent. What triggered the
crisis were not layoffs but an end of the rise in home prices." "Starting in mid-2006, foreclosures jumped
sharply for both prime and subprime ARMs, but not for fixed-rate mortgages of any kind, including subprime
ones." "ARMs draw a different kind of buyer, one who is often intent on selling or refinancing before rates
re-set." "... buyers ... made speculative loans or were intent on
flipping their homes, and they instead walked away from their mortgages at the first sign of home
depreciation." "... purchases of homes for investment purposes that the buyer didn't intend to live in,
amounted
to a whopping 28 percent of all deals, and 22 percent in 2006."
According to the chart
Real Estate Melt
Down, making it easier to obtain sub-prime mortgages lead to an increase housing pricing
relative to the average family income. Speculation as well as the laws of supply and
demand would easily justify such a scenario.
What we end up with is a situation in which the home owners who got in early, have nice
properties to their credit. Those in late couldn't ride the gravy train and got tossed
overboard. This affects/affected builders, mortgage companies, bankers, and ultimately the
general public due to the fact that whole statue of gold was attempting to be supported
through feat of clay.
The stock market suffered as a result. Long term investors have felt this most
tellingly. However, for those who know how to play the
market in both up and down modes, are making huge sums of money through the market
volatility. I've done some manual trades on both sides and have seen some appreciation, but
I wish I was much better at seeing the possibilities.
Anyway, as a summary to the article regarding risk gone bad, in 2003 the government
already
knew about the issue, but due to partisan interests across the board, was unable to
do anything:
History teaches that even the best minds in financial management cannot entirely eliminate
risk. This was shown quite clearly by the severe difficulties encountered by Long-Term
Capital Management several years ago. Nor do the GSE shareholders have the incentive to call
for eliminating risk. The perception of a government bailout if things go wrong surely
enhances any firm's willingness to take on risk and enjoy the associated increase in return.
The savings and loan crisis of the 1980s illustrates the adverse incentive effects that can
arise as a result of government guarantees.
In an
A Letter to Senator Obama by Tony Batman, he makes a very enlightening remark:
In other words, whatever you tax, you get less of; whatever you subsidize, you get more of.
The implication of this remark is that we need to somehow remove subsidies and come up with more creative
mechansims for balancing the perceived inequalities in the market place.
Later in the same article, one possible solution is mentioned:
Increased taxes on the so-called 'rich' high income earners - and their businesses will affect the incomes
of those who strive to move up from lower and middle classes to become high income earners!
In follow up to my mention of subsidy elimination a few paragraphs ago, another article mentiones that
We Need Reagan + Friedman + Keynes. In summary, "during periods of crisis, sometimes you have to be a
supply-sider (tax rates), sometimes a monetarist (Fed money supply), and sometimes a Keynesian (federal
deficits).", ie, "Choose the best policies as put forth by the great economic philosophers without being too
rigid."
[/Personal/Business]
permanent link
|