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…

Cornell Graduate Students United: At What Cost?

On Monday and Tuesday, we graduate students at Cornell will be voting on whether or not we want to unionise. Actually, scratch that, only graduate students who hold a TA, RA, or GRA appointment can unionise.This is a shitty arrangement, and I will be voting against it.For those of you who are not aware of how graduate school works at Cornell, you could be on one of many appointments.FellowshipA graduate student on a fellowship gets a stipend and tuition paid without associated teaching or research opportunities. Graduate students on a fellowship typically work towards their own theses, but will be excluded from the unionGraduate research assistantshipsA GRA gives a graduate student stipend and tuition without teaching responsibilities. However, this money comes out of a specific project grant, and the students typically work on their own theses. Students on GRAs magically qualify to join the union, whereas there is virtually no difference between a GRA and a fellowship for the most pa…

De-addicting

I’m on a process to break away from the distractions of social media, primarily Facebook, and spend the time to pursue other interests. This is an interesting experiment, and it has required significant dedication and effort on my part. At the end, though, I’m not certain if I’ll be successful.I realised that I had a problem when Facebook became the most visited sites in my browser. I should have been working, not scrolling through Facebook like a, well, you know what Jerry Seinfeld has to say. I further noticed that I would tend to log on to Facebook every time I was stuck at work, so that my capacity to tackle problems was reduced to only those that involved cracking a walnut shell. Anything more complex, and I would procrastinate and end up on Facebook.What I needed was a swift kick in the arse to get off Facebook and into the real world. It came through the means of my latest project which required all my attention. At this time, I logged out of Facebook from my work computer and …