Skip to main content

On writing platform independent code (or, why I like the new C++)

I use Linux when I work from home, I'm forced to use a Mac at work (well, I boot up a virtual Linux OS), and I use Windows when I just want to goof around with my computer. So, while most of my work is done on Linux, it's imperative that my code work on all platforms; just because I could use any of the three.

Traditionally, C required multiple versions of code, protected by #ifdefs. This often required multiple versions of code to be written, depending on the target system, target OS, and compiler being used. Clumsy and messy system.

C++ too had similar shortcomings. When it came to writing multi-threaded code, I had to choose either Win32 or Posix, and once I made that choice, I was bound by it. Since those were the days when Ubuntu was driving me crazy, I chose Win32. Bad decision.

Every single action that I attempted was compounded by the fact that Win32 is the worst API ever. How do I lock a mutex? Well, first I declare a handle, then declare a mutex, then define the handle to point to the mutex, then attempt to lock the mutex, specifying a timeout interval, then check to see if the error on the acquisition is ERROR_SUCCESS. A crazy system which leads to crazy code.

And that's not compatible with Posix, which is a much cleaner API.

So, when C++11 was announced, I jumped with joy at the fact that multi-threading support was built into the language, and that the proposed interface was so much similar to the cleaner Posix API. C++11 allows me to get rid of the system dependent multi-threading APIs, and focus on the code at hand that actually solves the problem. Not only that, C++11 specifies memory models for atomic operations; which allows me to atomically load, store and swap values. The only way this would be possible prior to C++11 was to declare a mutex for every atomic operation. Not a good idea, as it would lead to a tonne of mutexes, with large, irrelevant scope.

The other, messier option would be to dive down into the assembly level of the target platform, and write in some assembly to atomically load or store some values.

Combine the improved multi-threading and memory models in C++11 with CMake, and I get a nice cross platform code, which works on multiple platforms; well, almost. To be really sure, I need to test the code on each platform; but it's relatively harder to mess up, the most chances occur in CMake, where I need to define compiler options for different build environments using a number of conditional statements. It's worse, because CMake is scripted, and that means that there may be conditions which are written syntactically incorrect, but I would not be aware of this until I actually tried to build on a system that leads to those conditions. Still, CMake does not, or should not make a bulk of the code.

The usefulness of writing platform independent code was apparent when I worked on EmoDetect, with +Abhinandan Majumdar, +rishabh animesh, and +Aayush Saxena. I used Linux (Ubuntu 12.04), Abhinandan used Mac and Ubuntu (12.10), and both rishabh and Aayush used Windows (different versions here). Yet, we could collaborate perfectly (again, almost; the three of them had an inexplicable aversion to git, so we ended up passing files (not just patches (OMG!))).

I'd say that it's so important to write platform independent code. I've been trying (unsuccessfully) to port Darktable to Windows; and while I'm sure that it would not be too much effort to port the actual DT code, I'm stuck with compiling libraries, all of which were written for GNU, and Windows support was added later as a hack. Some of them don't compile, many need to be fixed, and that's holding up the process indefinitely.

That and the fact that I now almost always use Ubuntu, which means that I don't really bother about Windows software any longer.

Comments

Popular posts from this blog

On Harry Potter and why I dislike the series

There could not be a better time for this post. There could not have been a worse time for this post. Now that the penultimate movie of the series is out, and my facebook wall filled with people who loved the movie. But this is something I really wanted to say, and I shall say it anyway. Harry Potter is pathetic literature. Now, you must be wondering why I say that. There are many reasons. Firstly, the storyline itself is flawed. When a writer sits down to write anything, he/she must set up some essential rules about what is happening. These rules must remain constant irrespective of how many times he/she changes his/her mind. This is so that the readers are allowed to have some sensibility in what they are reading. In the fourth book, Rowling goes ahead and kills Cedric. Then, at the end of the book, the horseless carriages are there again. Nothing special. We all knew that they are horseless. But then comes the fifth book, and BAM, the horses are actually winged beasts that only thos...

On the Dvorak Simplified Keyboard

This is a post that I have been meaning to write from quite some time. Long hours spent typing code on my computer left my hands fatigued, and left me with a lot of pain in my wrists and fingers. That is when I decided to use the Dvorak. But I have got the same bad habit as Dr. Watson, to tell a story backwards. Of course, you must be wondering what the Dvorak is. The story of keyboards starts with the invention of the typewriter. Christopher Sholes, the inventor of the typewriter, tried with a two row piano style keyboard. But then, he got into many difficulties with the design. Then he finally settled for a four row design. This was similar to the QWERTY layout that most computers and typewriters today possess. The engineers at Remington, to whom Sholes had presented his design modified the layout a little further, and then the QWERTY was born. As typewriters became popular, people got used to the layout, and started practising touch typing, i.e. typing without looking at the keys...

The paradox of government

I'm fascinated by the concept of government, and the paradoxes it presents. On one hand, governments grant us a certain set of rights or liberties. On the other hand, they work to strip us of the very liberties they promise. Now, I don't mean that all governments strip people of liberties, but there are liberal regimes, and there are sufficiently restrictive and dictatorial ones. Both models may have results to show, it does not mean that people in a restrictive regime are unhappy (refer to Dan Dennett's TED talk , where he states that ideas or memes can be dangerous when taken from one part of the world, where they are widespread, and, using the virus analogy, where people are immune to the memes; to a part of the world where they are foreign, where people may not be immune to the memes and where people may get infected). History has shown that people were sufficiently satisfied with autocratic governments with a benevolent dictator, and that people in other parts of the ...