Monday, August 13, 2012

Exceptions and Destructors: The Saga Continues

Still on the subject of exceptions and destructors, I wondered what is the behavior when using threads. Note that exceptions do not propagate across threads, so catching exceptions in your main thread is not enough. You also have to catch in each and every thread.
Let's take the sample from the original article and introduce threads with a little help of Boost:

#include <iostream>
#include <stdexcept>
#include <boost/thread.hpp>

struct A {
  A () { std::cout << "A\n"; }
  ~A () { std::cout << "~A\n"; }
};

void myfunc() {
  A temp;
  throw std::runtime_error("moo");
}

void mythread() {
  myfunc();
}

int main() {
  try {
    boost::thread t(mythread);
    t.join();
  }
  catch (...) {
  }
}

This still will not call the destructor unless you catch in the thread:

void mythread() {
  try {
    myfunc();
  }
  catch (...) {
  }
}

So, it is a good practice to have a catch clause for all your threads.

With Visual C++ 11 and Gnu C++ 4.7.1 (with --std=c++11 option) std::thread class you don't need the catch clause to get the destructor called. I couldn't find if this is standard or a side effect of the implementation of std::thread on C++ standard libraries for those compilers.

Friday, August 10, 2012

GCC macros for portable development

While GCC define a large number of macros only some of them are useful for portable development. The use of pre-processor macros is a well-known technique for selecting which portions of your code can only be compiled on a specific platform.

The most interesting macros are:

  • __GNUC__: always defined for GCC
  • __GNUG__: always defined for G++
  • WIN32: defined on Windows only

This code:

#ifdef __GNUC__
  #ifdef WIN32
    std::cout << "GCC on Windows\n";
  #else
    std::cout << "GCC not on Windows\n";
  #endif
#endif

Outputs:

GCC on Windows

when built with MinGW on Windows and

Outputs:

GCC not on Windows

when built with GCC in other platforms.

Alternatively with MinGW you can also use __MINGW32_VERSION which is defined if _mingw.h is directly or indirectly (e.g. by including any standard header file) included.


Wednesday, August 08, 2012

Monday, August 06, 2012

Why the MinGW Distro? Some interesting background

In 2011 the new C++ standard was about to be released. I then challenged myself to study it, if not the entire beast at least the most interesting parts. This of course demanded a compiler with the latest features of the new standard. At that time the only compiler available that was being updated frequently was GCC. But while it was released very often the MinGW port was not so fast. So, I realized that the best way was to build it myself. A fast search and I found Stephan T. Lavavej's build. It has everything I needed and more. OK. But I don't need all that more so I took his distro, removed some things, added others. And why not release it for others? Then it evolved from a small packaged archive with instructions on how to setup it to a simple, easy-to-use installer. I had already some experience with InnoSetup and it was simple to build an setup package.
And now we are here. Do you have any comments or suggestions to the distro? Things you'd like to see? Please let me know. While I have limited time and resources to play with it I promise to kindly evaluate any request.
Visit the MinGW Distro homepage to download the latest version.