Archive for C/C++

C++ gotcha

I came across an amusing example in Meyers’ Effective STL Item 6***.

class Widget { ... };
Widget w();

As Meyers explains,  it seems like the default constructor is called. However, in C++ this second line is actually declaring a function ‘w’ that takes no parameters and returns a ‘Widget’. If the code author intended for the constructor to be called, he would have to write:

class Widget { ... };
Widget w;

I chuckled to myself after I saw this because the former will cause the Java compiler to get angry. This is another example of how you have to pay attention when you write C++.

*** I love Meyer’s works and his writing style.  He writes to you as a professional and an equal, even though he is a master.

Leave a Comment

Lock-Free Queue articles on Dr. Dobbs Journal

There are not a lot of websites and resources for C++ news these days.  Occasionally, Dr. Dobb’s Journal has some interesting articles, such as the Concurrent Queue seiries. So far there are three articles in response to the original idea, and it looks like more are to follow.

Lock-Free Queues – original idea for a lock-free queue
Lock-Free Code: A False Sense of Security – Herb Sutter’s first in a series of articles on a concurrent queue implementation
Writing Lock-Free Code: A Corrected Queue – Herb Sutter’s second article in the series
Writing a Generalized Concurrent Queue – Herb Sutter’s third article in the series
Somewhat related, there are also some interesting articles on threading as well:

What’s New in Boost Threads by Anthony Williams
Managing Application Thread Use by Levent Akyil
The Trouble With Locks – an older article by Herb Sutter on locks

Leave a Comment

Get up to speed on C++0x multithreading

DevX is running an artcile titled Simpler Multithreading in C++0x by Andrew Williams.  It is a very good introduction the multithreading supported that is to be present in C++0x.

Leave a Comment

“C++ Concurrency in Action” coming next year

Anthony Williams, author of the Boost threads library, is writing a book on C++ threading based on the upcoming thread support to be added to C++0x.  I am definitely going to pick it up when it comes out on paper.  Hopefully by the time the book comes out, the compiler developers will have a robust implementation of the threading libraries.

After years of messing with PThreads and Win32 threads, Boost threads have really made my life easier.  Boost threads are simple and they sure help make your code a lot more readable.  The only problem with Boost is that you have to install it separately and that can require a lot of researching and trial-and-error on older hardware and non-standard architectures.  It will be nice to have threading built into the language standard like in Ada and Java.

Comments (2)

Boost 1.36 and Sun Studio 12

Trying to get Boost and Sun Studio C++ compilers to place nice on Ultrasparc processors isn’t exactly straight-forward.  I fought with both Sun CC and g++, and after browsing mailing lists of Boost and other C++ software, reading various blogs at Sun, and through brute force effort, I managed to build Boost 1.36 for Sun CC.  I just have to add that it was a lot easier getting Boost to build on Mac OS X with g++!  I’m sure someone will say it I’m and idiot since I couldn’t get it to work ont he first try, but let me just say that it was not obvious.  I did a lot of reading and still did not come away with a definitive answer.  The guys at Sun ought to create a Wiki for both Sun Studio users and Sun engineers to share information on working with Boost.

Before I get into the build process, here is the build environment I used just for reference:

# uname -a
SunOS radium 5.10 Generic_137111-02 sun4u sparc SUNW,Sun-Blade-100

# CC -V
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-06 2008/07/08

Of course it is always a good idead to make sure your Sun Studio patches are up-to-date before you start.  The build process I used is listed below.

1. Build a custom version of Bjam for your machine.  Download it from Boost’s sourceforge portal and follow the install directions.  It builds painlessly and installs itself in /usr/bin by default.

2. After downloading and unarchiving Boost 1.36 to a stating platform–I used /opt–change your working directory to the Boost root directory and run the configuration script.  Run ./configure –help to see the various options.  I used the following to specify the location of bjam, the compiler to use, and finally where to put everything in the end:

./configure –with-bjam=/usr/bin/bjam –with-toolset=sun –prefix=/opt

It is important to specify the compiler if you have both g++ and Sun CC in your path environment variable.  On a default developer’s install of Solaris 10, you have g++ in /usr/sfw/bin and then if you installed Sun Studio 12, a symlink is placed in /usr/bin that points to CC.  Be careful!

3. Configure your user-config.jam file which is used by Bjam when building Boost.  After running the configure script, my user-config.jam looked like this:

# Boost.Build Configuration
# Automatically generated by Boost configure

# Compiler configuration
using sun ;

# Python configuration
using python : 2.4 : /usr ;

I added some flags for the compiler to fix a lot of the error messages I was getting when I would run the make process.  The modified file looks like this:

# Boost.Build Configuration
# Automatically generated by Boost configure

# Compiler configuration
using sun : 5.9 : /opt/SUNWspro/bin/CC : <stdlib>sun-stlport <link-flags>-library=stlport4 <cxxflags>-native64 ;

# Python configuration
using python : 2.4 : /usr ;

I should add that I don’t know if this is necessary based on step four in my process.  I really did not feel like trying to hack around the build process as it is complicated and honestly, I want to work on my application, not on becoming a guru Boost builder.  I made this changes because it is suggested on many of the mailing list troubelshooting messages.

4.  Finally, run Bjam to build your application.  On the Boost homepage it says you can run “make install”, but I found whenever I did this the build process would fail to compile any of the libraries.  I explicity invoked the Bjam tool as follows:

bjam toolset=sun stdlib=sun-stlport instruction-set=v9 address-model=64 install

This command may be redundant based on the parameters in user-config.jam, but I just wanted to be sure.  I turned off the monitor and went to bed because it takes a long time to build software on a Sun Blade 100!  Before leaving for work the next morning I checked a log file that I dumbed all of the build process output to and this time I had no errors.  The libraries were put in /opt/lib and my headers in /opt/include.  All is well that ends well.  Now it is time to put Sun’s compiler and tools to work!

Comments (1)