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.

Popular posts from this blog

Progressive Snapshot: Is it worth it?

I turned 25 last year, which in the highly mathematical and calculating eyes of the US insurance industry meant that I had suddenly matured into a much more responsible driver than I was at 24 years and 364 days of age. As a result, I expected my insurance rates to go down. Imagine my surprise when my insurance renewal notice from GEICO actually quoted a $50 increase in my insurance rates. To me, this was a clear signal that it was time to switch companies.Typically, I score really high on brand loyalty. I tend to stick with a brand for as long as possible, unless they really mess up. This qualified as a major mess up. As a result, I started shopping for insurance quotes.Two companies that quoted me significantly lower rates (30%–40% lower) were Progressive and Allstate. Both had an optional programme that could give me further discounts based on my consenting to the companies tracking my driving habits. Now, I am a careful driver – I hardly ever accelerate hard. I hate using the brak…

Build those noise cancelling headphones

So, here's another DIYLet me start by putting the cart before the horse. I shall start with the credits. This project was done while I was working on my Electronics Design Lab, along with my friends, Srujan M and Indrasen Bhattacharya. The work would not have been possible without the generous help received from the staff at Wadhwani Electronics Laboratory, who ensured that the only thing we did right was to leave the lab on time. This project would also not have been possible without the guidance of our dear and learned professors. It would probably have just about become additional dead weight on the head.Enough with the credits, now, I need to dive right into noise cancellation and how it works.The essence of sound is a pressure wave. The pressure wave, when incident on the eardrum sets into motion the complex mechanisms inside the ear, and after a long path, rather like the Cog advertisement, ends up making some nerves vibrate. The nerves send electrical signals to the brain, …

Reading List, December 2017

Brian Merchant, How email open tracking quietly took over the world, in Wired, 11 December 2017. [Online]: is no longer a secret that every website you visit silently tracks you in an effort to maximise ad revenue. What is less known is that emails also track you, through the use of tracking pixels and redirect links. These techniques were used by spammers and legitimate companies alike when creating newsletters or other mass email, in order to figure out their reach. What’s happening now is that private people are also using these techniques in order to create invisible and intrusive read receipts for email, which is incredibly frustrating from a privacy point of view.My solution to the tracking woes? I only open the plain-text component of email, which gets rid of tracking pixels entirely. Redirect links are harder to beat, and I don’t have a good solution for this.Dan Luu, Computer latency 1977–2017. D…