Search

ReCode /

It's Not Just About Memory!

I stumbled upon a very interesting post on concurrency. The author criticizes the transactional memory approach to simplifying multi-threading programming, compared to classical lock-based multi-threading models. Indeed, it is easy for a novice programmer to create a mess when using locks, the typical feared result being a system that exhibits random, very difficult to diagnose crashes and deadlocks.

What caught my eye is the focus of the author on a single reason why the TM model is flawed: complex software systems do not merely operate on memory.

This strikes me as important because I've observed a similar issue with garbage collection systems and languages. Like multi-threading programming, dealing with memory is difficult and it is easy for novice programmers to end up with a unmanageable mess of dangling pointers and memory leaks. And the solution? Simply don't worry about memory -- use as much as you need whenever you need it, the garbage collector will do its magic and it'll just work.

The problem with garbage collection systems is exactly the same: complex software systems do not merely operate on memory. Indeed, garbage collectors themselves do not manage memory. They manage objects lifetimes.

In the twisted mind of a Java developer we shouldn't worry about when the object will be collected: it can lurk around until an opportune moment arrives when the system has nothing better to do, and then it'll take care of it; and because the only type of resource most objects acquire is memory, most of the time we don't care when the object will be collected.

Yet, in the real world we must deal with all kinds of resources: files, sockets, locks, etc. We can't be sloppy with their acquisition and release. Their lifetime is just as hard to manage as memory and it is just as easy for a novice programmer to create a mess.

Solve that problem with finally and you're back to square one.

Steve — 01 August 2010, 08:05

I recently attended a code review of a rather convoluted C++ scheme (with timers, threads, and smart pointers) for acquiring and releasing a resource in a timely manner. I saw that everything of interest happened between a pair of curly braces and suggested wrapping the code (in this case a bunch of ODBC) with a simple stack-based object (RAII). I was taken aback by their response that this was 'Java-based thinking' and that I was trying to 'do garbage collection in C++', revealing no understanding of the difference between eventual vs. deterministic freeing of resources.

Emil — 03 August 2010, 16:17

Right, this is typical for programmers who learned C or C++ after they've had some experience writing Java-style code. Even the ones who do understand the semantics of local objects in C++ don't use them because they feel alien to them.

And so they use smart pointers to solve an artificial problem they created themselves. :)

OvermindDL1 — 16 August 2010, 16:31

I would prefer to handle memory like I handle files and sockets and such in C++ as I would handle them in Erlang. Erlang has no memory sharing between threads, that vastly simplifies things and allows Erlang to perform a variety of optimizations. You should take a look at that and write a blog about your thoughts (not the language of Erlang, prolog-like, but the style it operates in, pure Actor-oriented).

Add comment: 
Sign as author: 
Add 1 to 702 and enter it here: 

Formatting hint: when posting comments, surround code blocks in [@ and @].