Effective Concurrency: Prefer Using Active Objects Instead of Naked Threads

This month’s Effective Concurrency column, “Prefer Using Active Objects Instead of Naked Threads,” is now live on DDJ’s website.

From the article:

… Active objects dramatically improve our ability to reason about our thread’s code and operation by giving us higher-level abstractions and idioms that raise the semantic level of our program and let us express our intent more directly. As with all good patterns, we also get better vocabulary to talk about our design. Note that active objects aren’t a novelty: UML and various libraries have provided support for active classes. Some actor-based languages already have variations of this pattern baked into the language itself; but fortunately, we aren’t limited to using only such languages to get the benefits of active objects.

This article will show how to implement the pattern, including a reusable helper to automate the common parts, in any of the popular mainstream languages and threading environments, including C++, C#/.NET, Java, and C/Pthreads.

I hope you enjoy it. Finally, here are links to previous Effective Concurrency columns:

1 The Pillars of Concurrency (Aug 2007)

2 How Much Scalability Do You Have or Need? (Sep 2007)

3 Use Critical Sections (Preferably Locks) to Eliminate Races (Oct 2007)

4 Apply Critical Sections Consistently (Nov 2007)

5 Avoid Calling Unknown Code While Inside a Critical Section (Dec 2007)

6 Use Lock Hierarchies to Avoid Deadlock (Jan 2008)

7 Break Amdahl’s Law! (Feb 2008)

8 Going Superlinear (Mar 2008)

9 Super Linearity and the Bigger Machine (Apr 2008)

10 Interrupt Politely (May 2008)

11 Maximize Locality, Minimize Contention (Jun 2008)

12 Choose Concurrency-Friendly Data Structures (Jul 2008)

13 The Many Faces of Deadlock (Aug 2008)

14 Lock-Free Code: A False Sense of Security (Sep 2008)

15 Writing Lock-Free Code: A Corrected Queue (Oct 2008)

16 Writing a Generalized Concurrent Queue (Nov 2008)

17 Understanding Parallel Performance (Dec 2008)

18 Measuring Parallel Performance: Optimizing a Concurrent Queue (Jan 2009)

19 volatile vs. volatile (Feb 2009)

20 Sharing Is the Root of All Contention (Mar 2009)

21 Use Threads Correctly = Isolation + Asynchronous Messages (Apr 2009)

22 Use Thread Pools Correctly: Keep Tasks Short and Nonblocking (Apr 2009)

23 Eliminate False Sharing (May 2009)

24 Break Up and Interleave Work to Keep Threads Responsive (Jun 2009)

25 The Power of “In Progress” (Jul 2009)

26 Design for Manycore Systems (Aug 2009)

27 Avoid Exposing Concurrency – Hide It Inside Synchronous Methods (Oct 2009)

28 Prefer structured lifetimes – local, nested, bounded, deterministic (Nov 2009)

29 Prefer Futures to Baked-In “Async APIs” (Jan 2010)

30 Associate Mutexes with Data to Prevent Races (May 2010)

31 Prefer Using Active Objects Instead of Naked Threads (June 2010)

Effective Concurrency Course: June and (Not) October

I forgot to blog about this until now because of focusing on the Effective Concurrency course in Stockholm a few weeks ago, but to answer those who wonder if I’ll be giving it again in North America too: Yes, I’m also giving the public Effective Concurrency course again at the end of this month at the Construx facility in Bellevue, WA, USA. This will be the full four-day version of the course. Spaces are still available.

I’ll cover the following topics:

  • Fundamentals: Define basic concurrency goals and requirements • Understand applications’ scalability needs • Key concurrency patterns
  • Isolation — Keep work separate: Running tasks in isolation and communicate via async messages • Integrating multiple messaging systems, including GUIs and sockets • Building responsive applications using background workers • Threads vs. thread pools
  • Scalability — Re-enable the Free Lunch: When and how to use more cores • Exploiting parallelism in algorithms • Exploiting parallelism in data structures • Breaking the scalability barrier
  • Consistency — Don’t Corrupt Shared State: The many pitfalls of locks–deadlock, convoys, etc. • Locking best practices • Reducing the need for locking shared data • Safe lock-free coding patterns • Avoiding the pitfalls of general lock-free coding • Races and race-related effects
  • High Performance Concurrency: Machine architecture and concurrency • Costs of fundamental operations, including locks, context switches, and system calls • Memory and cache effects • Data structures that support and undermine concurrency • Enabling linear and superlinear scaling
  • Migrating Existing Code Bases to Use Concurrency
  • Near-Future Tools and Features

I hope to see some of you there!

Update 8/19: I had planned to do the EC course again in October (hence the post title) but that now has to be deferred to sometime in the new year. Sorry for any inconvenience to those of you who had already registered, or were planning to. I’ll blog about it here when it’s rescheduled.

Webinar Now Available On Demand

The webinar I did with James Reinders three weeks ago is now online for on-demand viewing. The link is the same as before:

Five Years Since Free Lunches: Making Use of Multicore Parallelism

Reflecting on the five years since "The Free Lunch is Over" article and the arrival of multicore processors, Sutter and Reinders will talk about the keys to making effective use of multicore parallelism. Programmers are in the midst of an evolution that takes time, and we are still in the early days. Sutter and Reinders share expert advice on how to view this transition, and the keys to getting it right—including a three-pillar model for concurrency, the need to maximize the number of tasks expressed in a program, and how abstractions encourage "many task" programming, while allowing overheads to be driven down under-the-covers.

Intel wants you to register (which is free), then you can watch the video and listen to the live (and sometimes quite lively) Q&A we held at the end. I hope you enjoy it!

Note: When we did it live, the audio was missing for part of the presentation. I’m told the glitch has been fixed, and the complete video, soundtrack and all, is available in the on-demand version.

C++ and Beyond: About 2/3 Full

C++ and Beyond 2010 (October 24-27) is filling up quickly. As of this writing, nearly 40 of the 60 places have been taken since registration opened last month.

If you’re thinking of registering, it would probably be good to do it soon. 60 attendees is a hard limit; as I’ve written before, we want to have lots of face time with individual attendees. So once we hit 60, registration will close, although we’ll probably set up a waiting list for those of you that don’t quite make it into the first 60 so that you’ll get dibs if there are any cancellations.

I’m looking forward to seeing many of you in beautiful Snoqualmie, WA, USA this fall.

Effective Concurrency: Associate Mutexes with Data to Prevent Races

This month’s Effective Concurrency column, Associate Mutexes with Data to Prevent Races”, is now live on DDJ’s website.

From the article:

Come together: Associate mutexes with the data they protect, and you can make your code race-free by construction

Race conditions are one of the worst plagues of concurrent code: They can cause disastrous effects all the way up to undefined behavior and random code execution, yet they’re hard to discover reliably during testing, hard to reproduce when they do occur, and the icing on the cake is that we have immature and inadequate race detection and prevention tool support available today.

The holy grail of the Consistency pillar is to make a concurrent program race-free and deadlock-free by construction. … This article shows how to achieve the "race-free by construction" grail via sound engineering, with automatic and deterministic race identification at test time based on code coverage alone.

I hope you enjoy it. Finally, here are links to previous Effective Concurrency columns:

1 The Pillars of Concurrency (Aug 2007)

2 How Much Scalability Do You Have or Need? (Sep 2007)

3 Use Critical Sections (Preferably Locks) to Eliminate Races (Oct 2007)

4 Apply Critical Sections Consistently (Nov 2007)

5 Avoid Calling Unknown Code While Inside a Critical Section (Dec 2007)

6 Use Lock Hierarchies to Avoid Deadlock (Jan 2008)

7 Break Amdahl’s Law! (Feb 2008)

8 Going Superlinear (Mar 2008)

9 Super Linearity and the Bigger Machine (Apr 2008)

10 Interrupt Politely (May 2008)

11 Maximize Locality, Minimize Contention (Jun 2008)

12 Choose Concurrency-Friendly Data Structures (Jul 2008)

13 The Many Faces of Deadlock (Aug 2008)

14 Lock-Free Code: A False Sense of Security (Sep 2008)

15 Writing Lock-Free Code: A Corrected Queue (Oct 2008)

16 Writing a Generalized Concurrent Queue (Nov 2008)

17 Understanding Parallel Performance (Dec 2008)

18 Measuring Parallel Performance: Optimizing a Concurrent Queue (Jan 2009)

19 volatile vs. volatile (Feb 2009)

20 Sharing Is the Root of All Contention (Mar 2009)

21 Use Threads Correctly = Isolation + Asynchronous Messages (Apr 2009)

22 Use Thread Pools Correctly: Keep Tasks Short and Nonblocking (Apr 2009)

23 Eliminate False Sharing (May 2009)

24 Break Up and Interleave Work to Keep Threads Responsive (Jun 2009)

25 The Power of “In Progress” (Jul 2009)

26 Design for Manycore Systems (Aug 2009)

27 Avoid Exposing Concurrency – Hide It Inside Synchronous Methods (Oct 2009)

28 Prefer structured lifetimes – local, nested, bounded, deterministic (Nov 2009)

29 Prefer Futures to Baked-In “Async APIs” (Jan 2010)

30 Associate Mutexes with Data to Prevent Races (May 2010)

The “You Call This Journalism?” Department

The Inquirer isn’t normally this silly, and it isn’t even April 1. Nick Farrell writes:

Why Apple might regret the Ipad [sic]

THE IPAD HAS DOOMED Apple, according to market anlaysts [sic] that are expecting the tablet to spell trouble for its maker. … Rather than killing off the netbook, the Ipad [sic] is harming sales of the Ipod [sic] and Macbooks… if the analysts are right the Ipad [sic] has killed the Ipod [sic] Touch.

This is just silly, for four reasons. Three are obvious:

  • The iPod Touch fits in your pocket and can be easily with you all the time. Nothing bigger can ever kill it, but only replace it for a subset of users who don’t need in-pocket portability. (Besides, even if all iPod Touch buyers bought an iPad instead, the latter is more expensive and so the correct term would be not “kill” but “upsell”.)
  • The laptop has a real keyboard and full applications. Nothing not full-featured can ever kill it, but only replace it for a subset of users who don’t need the richer experience and applications.
  • Even if it was killing the other business outright, which it isn’t, it’s always better to eat your own lunch than wait for a competitor to do it.

And the fourth reason it’s silly? Let’s be very clear: The iPad has sold 1 million units in its first 28 days. At $500-700 a pop, that means the iPad is becoming a new billion-dollar business in two months.

Nick, I don’t think “regret” is the word you’re looking for.

May 12 Webinar on Multicore Parallelism

Next week, I’m giving a webinar with Intel’s James Reinders, and we’ll be available for a live Q&A session with you at the end:

Five Years Since Free Lunches: Making Use of Multicore Parallelism

May 12, 2010 at 8 a.m. PT/11 a.m. ET.

Reflecting on the five years since "The Free Lunch is Over" article and the arrival of multicore processors, Sutter and Reinders will talk about the keys to making effective use of multicore parallelism. Programmers are in the midst of an evolution that takes time, and we are still in the early days. Sutter and Reinders share expert advice on how to view this transition, and the keys to getting it right—including a three-pillar model for concurrency, the need to maximize the number of tasks expressed in a program, and how abstractions encourage "many task" programming, while allowing overheads to be driven down under-the-covers.

James and I had a lot of fun recording the video portion about a month ago, and I hope you enjoy it. We touched on everything from the three basic pillars I’ve written about before but that bear repeating, to whether functional languages will take the world by storm (perhaps surprisingly, yes… but why and how?), to the perennial question of “how finely should I decompose my work to run it well in parallel” (and I really must write an EC article about this).

Afterwards, James and I will be on the phone to take your questions. If you have a particular point you want to ask about, why not take the time now to prepare a well-crafted and concise question in advance? I’m looking forward to hearing from many of you.

Links I enjoyed this week: Flash and HTML5

These are the two best links I’ve read in the wake of the Flash and HTML5 brouhaha(s). They discuss other informative points too, but their biggest value lies in discussing three things, to which I’ll offer the answers that make the most sense to me:

  • What is the web, really? “The web” is the cross-linked content, regardless of what in-browser/PC-based/phone-based generator/viewer/app is used to produce it and/or consume it.
  • Does web == in-browser? No. Native apps can be web apps just as much so as in-browser ones, and increasingly many native apps are web apps. Conversely, not everything that runs in a browser is part of the web, even though most of them are for the obvious historical reasons.
  • Is it necessary/desirable/possible to make in-browser apps be like native apps? No, maybe, and maybe. The jury is still out, but at the moment developers are still trying while some pundits keep decrying.

Here are the two articles.

Understand the Web (Ben Ward)

This rambly piece needs serious editing, but is nevertheless very informative. Much of the debate about Flash and/or HTML5 conflates two things: the web, and application development platforms. They aren’t the same thing, and in fact are mostly orthogonal. From the article:

Think about that word; ‘web’. Think about why it was so named. It’s nothing to do with rich applications. Everything about web architecture; HTTP, HTML, CSS, is designed to serve and render content, but most importantly the web is formed where all of that content is linked together. That is what makes it amazing, and that is what defines it.

… [in the confused Flash and HTML5 debates] We’re talking about two very different things: The web of information and content, and a desire for a free, cross-platform Cocoa or .NET quality application framework that runs in the browsers people already use.

On a different note, speaking of the desire for super-rich in-browser apps, he adds:

Personally, aside from all of this almost ideological disagreement over what the web is for, and what you can reasonably expect it to be good at, I honestly think that ‘Desktop-class Web Applications’ are a fools folly. Java, Flash, AIR and QT demonstrate right now that cross-platform applications are always inferior to the functionality and operation of the native framework on a host platform. Steve Jobs is right in his comments that third-party frameworks are an obstacle to native functionality.

HTML5 and the Web (Tim Bray)

Again, what “the web” is – and it has nothing specifically to do with HTML. From the article:

The Web is a tripod, depending critically on three architectural principles:

  • Pieces of the Web, which we call Resources, are identified by short strings of characters called “URIs”.

  • Work is accomplished by exchanging messages, which comprise metadata and representations of Resources.

  • The representations are expressed in a number of well-defined data formats; you can count on the message data to tell you which one is in use. It is essential that some of the representation formats be capable of containing URIs. The “Web” in WWW is that composed by the universe of Resources linked by the URIs in their representations.

That’s all. You notice that there’s nothing there that depends crucially on any flavor of HTML. Speaking only for myself, an increasingly large proportion of my Web experience arrives in the form of feed entries and Twitter posts; not HTML at all, but 100% part of the Web.

On Flash · This may be a side-trip, but anyhow: I entirely loathe Flash but by any definition it’s part of the Web. It works just fine as a resource representation and it can contain URI hyperlinks.

Native Applications · A large proportion of the native applications on iPhone, and on Android, and on Windows, and on Mac, and on Linux, are Web applications. They depend in a fundamental way on being able to recognize and make intelligent use of hyperlinks and traverse the great big wonderful Web.

… So whatever you may think of native applications, please don’t try to pretend that they are (or are not) necessarily good citizens of the Web. Being native (or not) has nothing to do with it.

Good stuff.

C++ and Beyond 2010: Registration Now Open

I’m happy to report that registration is now open for C++ and Beyond 2010 with me, Scott Meyers, and Andrei Alexandrescu. The event will start on the evening of Sunday October 24 with a reception, to be followed by three solid breakfast-to-bedtime days full of structured and unstructured technical content and learning opportunities in what is designed to be a C++-and-more immersion retreat.

Over the past few days, Scott has written up three nice informational pieces on the C&B blog:

  • C++ and Beyond event structure: How it’s not a conference, runs from morning till late night daily for in-depth technical immersion, and more.
  • The venue for C&B: A description of the event location and surroundings, focusing on what makes it a perfect fit for our kind of up-close-and-personal event.
  • Registration is now open! Quoting most of today’s announcement:

Groups of 3 or more get 10% off, and individual or group registration during the Early Bird period (now until July 24) knocks another 10% off the price.  Attendance is limited to 60, so if you’re interested in being part of C&B, I suggest you register as soon as you can.

Click here to register.

We hope to see you atop Snoqualmie Falls in October!

I’m looking forward to this information-packed and not-boring-conference event, and spending time with many of you one-on-one.

“Readability”

If you like reading just about anything on the web, including my articles, in a pretty nicely rendered plain format with no ads or other distractions, you might want to try out arc90’s Readability.

All you do is drag a bookmarklet to your bookmark bar, and then on any article-like web page you can click on the bookmarklet to turn this:

image

into this (with a few choices each for font, size, and margin):

image

This lets you gain a lot in readability when all you want to do is read the article itself with basic text and graphics rendered fairly nicely. You do lose a little formatting, such as colored text which I sometimes use in my articles’ code examples, but the overall effect is pretty nice.

I’ll keep trying Readability out, especially on smaller-than-desktop screens, to see if it’s a keeper. So far the overall effect is pretty nice. Thanks to James P. Hogan for the tip, even if the link his page gives is broken.

 

Note: If you’re using Mobile Safari (i.e., iPhone or iPad) you’ll need to do a little bit more work because that software doesn’t currently support dragging the bookmarklet to its bookmark bar. Fortunately, there’s a workaround:

  • Find the Javascript code. I just made the bookmarklet on a desktop browser and copied the code from there to an email to myself (some things are faster with a keyboard and mouse). Alternatively you can inspect the HTML using HTML Viewer right on the same device as Mobile Safari and cut-and-paste from that.
  • In Mobile Safari, make a new bookmarklet.
  • Edit it, and paste the Javascript code as the URL.

As has been true since the early Mac days in the 1980s, Apple products and SDKs make every piece of functionality either super easy if it’s supported, or super painful if it’s not. :-)

Pre-emptive snarky comment: Yes, I know some people will retort that Microsoft and Linux products are better, because at least they consistently make everything super painful all of the time… but I think that’s only half true.