Blog Archive

ACCU 2013 and the C++ Standards Meeting

Monday, 06 May 2013

This year's ACCU conference was at a new venue: the Marriott hotel in Bristol. This is a bit closer to home for me than the previous venue in Oxford, which made the trip there and back more comfortable. As ever, the conference itself was enjoyable, educational and exhausting in equal measure.

This year was also BSI's turn to host the Spring ISO C++ committee meeting, which was conveniently arranged to be the week following ACCU, in the same hotel. Having not attended a meeting since the last time the committee met in the UK, I was glad to be able to attend this too.

ACCU 2013

As usual, with 5 tracks running simultaneously, it was hard to choose which sessions to attend. I stuck mostly to C++-centric, or general software development sessions, but there were also sessions on a wide range of other topics, including Ruby, Java, Scala, Git, C#, testing, management and culture, amongst others.

I was invited to contribute to Pete Goodliffe's Becoming a Better Programmer panel session, which was well attended and entertaining, as usual for Pete's sessions. My contribution on "doing things mindfully" seemed well-received, but wasn't the most popular — that honour went to Seb Rose, though Brian Marick called out Anna-Jayne Metcalfe's contribution on "If it ain't broke, do fix it" in the keynote the next morning.

My presentation on "C++11 in the Real World" was also well attended, with some good questions from the audience. A couple of people have asked me for my slides: they are available from the ACCU website.

ISO C++ committee meeting

This was a landmark meeting, for several reasons. Firstly, there were over 100 attendees, making it one of the most well-attended ISO C++ meetings ever. Secondly, this bumper attendee count was complemented by a bumper batch of proposals and position papers to process, which meant that all the working groups were pressed for time, and people met for extra sessions in the evenings to try and get through them all. Finally, the committee voted to publish a new "CD", starting the formal process leading to a C++14 standard.

The meeting was 6 days long, but I was only able to attend for the first 2 days. Unsurprisingly, I spent my time in the Concurrency group (SG1). We had a lot of papers to discuss, and some of the discussions were quite involved. Ultimately, not many papers were forwarded to the full committee, and only one paper other than the basic defect-report fixes was approved.

Lawrence Crowl's paper on Stream Mutexes (N3535) was first up. The problem this paper is trying to address is ensuring that data written to a stream from multiple threads appears in a coherent order — though concurrent writes to e.g. std::cout are guaranteed not to yield undefined behaviour, the output may be interleaved in an arbitrary fashion. This got quite a bit of discussion over the course of the week, and was eventually submitted as a much-modified paper for writing chunks to a stream in an atomic fashion, which was voted down in full committee.

Herb Sutter's late paper on the behaviour of the destructor of std::future (N3630) was up next. This is a highly conterversial topic, and yielded much discussion. The crux of the matter is that as currently specified the destructor of std::future blocks if it came from an invocation of std::async, the asynchronous function was run on a separate thread (with the std::launch::async policy), and that thread has not yet finished. This is highly desirable in many circumstances, but Herb argued that there are other circumstances where it is less desirable, and this makes it hard to use std::future in some types of program.

Much of the discussion focused on the potential for breaking existing code, and ways of preventing this. The proposal eventually morphed into a new paper (N3637) which created 2 new types of future: waiting_future and shared_waiting_future. std::async would then be changed to return a waiting_future instead of a future. Existing code that compiled unchanged would then keep the existing behaviour; code that changed behaviour would fail to compile. Though the change required to get the desired behaviour would not be extensive, the feeling in the full committee was that this breakage would be too extensive, and the paper was also voted down in full committee.

Herb's original paper also included a change to the destructor of std::thread, so that the destructor of a joinable thread would call join() rather than std::terminate(). This was put up for vote as N3636, but again was voted down in full committee.

Like I said, there were lots of other papers up for discussion. Some were concrete proposals, whilst others were less complete, asking for feedback on the approach. Only one paper was approved for the C++14 time frame — whilst there was considerable interest in the idea behind some of the others, there was disagreement about the details, and nothing else was deemed ready. I look forward to seeing the revised versions of some of these proposals when they are ready, especially the executors, continuations and parallel algorithms papers.

The paper that did get approved was Howard Hinnant's paper on shared locking (N3568), but even that didn't go through unchanged. I have serious concerns about the upgrade_mutex proposed in the original paper, and while I didn't manage to get my concerns across via email (this was discussed after I left), there was not enough interest in including it in C++14. The approved paper (N3659) therefore included only shared_mutex and shared_lock, not upgrade_mutex, which is good. N3659 was also approved by the vote in full committee, so will be part of C++14.

Wrap up

Having the conference and ISO meeting back-to-back was intense, but I thoroughly enjoyed attending both. C++14 looks set to be a significant improvement over C++11 — though the individual changes are minor, they offer quite a bit in terms of improved usability of the language and library. See the trip reports by Herb Sutter and Michael Wong (part 2, part 3) for more details on the accepted papers.

Posted by Anthony Williams
[/ news /] permanent link
Tags: , , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

just::thread Pro: Actors Edition released

Tuesday, 23 April 2013

I am pleased to announce that the first release of Just::Thread Pro is here. The Actors Edition provides a framework for creating actors that run on separate threads, and communicate via message passing, as well as jss::synchronized_value for synchronizing access to a single object and jss::concurrent_map, a hash map that is safe for concurrent access from multiple threads.

See the overview for more information, or read the full documentation.

Get your copy of Just::Thread Pro: Actors Edition

Purchase your copy and get started now.

Posted by Anthony Williams
[/ news /] permanent link
Tags: , , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

ACCU 2013

Saturday, 06 April 2013

I'm presenting on "C++11 features and real-world code" at ACCU 2013 in Bristol on this coming Thursday, 11th April. Here's the abstract:

C++11 has many nifty features, but how do they actually impact developers at the code face? Which C++11 features offer the best bang for the buck?

In this session I'll look at a selection of C++11 language and library features that I've found of real practical benefit in application and library code, with examples of equivalent C++03 code.

The features covered will include the concurrency support (of course), lambdas, and "auto", amongst a variety of others.

Hope to see you there!

Posted by Anthony Williams
[/ news /] permanent link
Tags:

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

Duplication in Software

Tuesday, 26 March 2013

Much has been said about the importance of reducing duplication in software. For example, J. B. Rainsberger has "minimizes duplication" as the second of his four "Elements of Simple Design", and lots of the teachings of the Agile community stress the importance of reducing duplication when refactoring code.

Inspired by Kevlin Henney's tweet last week, where he laments that programmers trying to remove duplication often take it literally, I wanted to talk about the different kinds of duplication in software. I've just mentioned "literal" duplication, so let's start with that.

Basic Literal Duplication

This is the most obvious form of duplication: sections of code which are completely identical. This most often arises due to copy-and-paste programming, but can often arise in the form of repetitive patterns — a simple for loop that is repeated multiple places with the same body, for example.

Removing Literal Duplication

The easiest to create, literal duplication is also the easiest to remove: just extract a function that does the necessary operation.

Sometimes, though the code is identical, the types involved are different. You cannot address this with extracting a simple function, so we have a new class of duplication.

Parametric Literal Duplication

Parametric literal duplication can also arise from copy-and-paste programming. The key feature is that the types of the variables are different so you cannot just reuse the code from one place in another, even if it was a nicely self-contained function. If you eliminate all the basic literal duplication, parametric literal duplication will give you sets of functions with identical structure but different types.

With the lack of a portable is_ready() function for std::future, it is common to test whether a future f is ready by writing f.wait_for(std::chrono::seconds(0))==std::future_status::ready. Since std::future is a class template, the types of the various futures that you may wish to check for readiness may vary, so you cannot extract a simple function. If you write this in multiple places you therefore have parametric literal duplication.

Removing Parametric Literal Duplication

There are various ways to remove parametric literal duplication. In C++ the most straightforward is probably to use a template. e.g.

template<typename T>
inline bool is_ready(std::future<T> f){
    return f.wait_for(std::chrono::seconds(0))==std::future_status::ready;
}

In other languages you might choose to use generics, or rely on duck-typing. You might also do it by extracting an interface and using virtual function calls, but that requires that you can modify the types of the objects, or are willing to write a facade.

Parametric literal duplication is closely related to what I call Structural Duplication.

Structural Duplication

This is where the overall pattern of some code is the same, but the details differ. For example, a for loop that iterates over a container is a common structure, but the loop body varies from loop to loop.e.g

std::vector<int> v;

int sum=0;
for(std::vector<int>::iterator it=v.begin();it!=v.end();++it){
    sum+=*it;
}
for(std::vector<int>::iterator it=v.begin();it!=v.end();++it){
    std::cout<<*it<<std::endl;
}

You can't just extract the whole loop into a separate function because the loop body is different, but that doesn't mean you can't do anything about it.

Removing Structural Duplication

One common way to remove such duplication is to extract the commonality with the template method pattern, or create a parameterized function where the details are passed in as a function to call.

For simple loops like the ones above, we have std::for_each, and the new-style C++11 for loops:

std::for_each(v.begin(),v.end(),[&](int x){sum+=x;});
std::for_each(v.begin(),v.end(),[](int x){std::cout<<x<<std::endl;});

for(int x:v){
    sum+=x;
}
for(int x:v){
    std::cout<<x<<std::endl;
}

Obviously, if your repeated structure doesn't match the standard library algorithms then you must write your own, but the idea is the same: take a function parameter which is a callable object and which captures the variable part of the structure. For a loop, this is the loop body. For a sort algorithm it is the comparison, and so forth.

Temporal Duplication

This is where some code only appears once in the source code, but is executed repeatedly, and the only desired outcome is the computed result, which is the same for each invocation. For example, the call to v.size() or v.end() to find the upper bound of an iteration through a container.

std::vector<int> v;
for(unsigned i=0;i<v.size();++i)
{
    do_stuff(v[i]);
}

It doesn't just happen in loops, though. For example, in a function that inserts data into a database table you might build a query object, run it to insert the data, and then destroy it. If this function is called repeatedly then you are repeatedly building the query object and destroying it. If your database library supports parameterization then you may well be able to avoid this duplication.

Removing Temoral Duplication

The general process for removing temporal duplication is to use some form of caching or memoization — the value is computed once and then stored, and this stored value is used in place of the computation for each subsequent use. For loops, this can be as simple as extracting a variable to hold the value:

for(unsigned i=0,end=v.size();i!=end;++i){
    do_stuff(v[i]);
}

For other things it can be more complex. For example, with the database query example above, you may need to switch to using a parameterized query so that on each invocation you can bind the new values to the query parameters, rather than building the query around the specific parameters to insert.

Duplication of Intent

Sometimes the duplication does not appear in the actual code, but in what the code is trying to achieve. This often occurs in large projects where multiple people have worked on the code base. One person writes some code to do something in one source file, and another writes some code to do the same thing in another source file, but different styles mean that the code is different even though the result is the same. This can also happen with a single developer if the different bits are written with a large enough gap, such that you cannot remember what you did before and your style has changes slightly. To beat the loop iteration example to death, you might have some code that loops through a container by index, and other code that loops through the same container using iterators. The structure is different, but the intent is the same.

Removing Duplication of Intent

This is one of the hardest types of duplication to spot and remove. The way to remove it is to refactor one or both of the pieces of code until they have the same structure, and are thus more obviously duplicates of one-another. You can then treat them either as literal duplication, parametric literal duplication or structural duplication as appropriate.

Incidental Duplication

This is where there is code that looks identical but has completely a different meaning in each place. The most obvious form of this is with "magic numbers" — the constant "3" in one place typically has a completely different meaning to the constant "3" somewhere else.

Removing Incidental Duplication

You can't necessarily entirely eliminate incidental duplication, but you can minimize it by good naming. By using symbolic constants instead of literals then it is clear that different uses are distinct because the name of the constant is distinct. There will be still be duplication of the literal in the definition of the constants, but this is now less problematic.

In the case that this incidental duplication is not just a constant then you can extract separate named functions that encapsulate this duplicate code, and express the intent in each case. The duplication is now just between these function bodies than between the uses, and the naming of the functions makes it clear that this is just incidental duplication.

Conclusion

There are quite a few types of duplication that you may get in your code. By eliminating them you will tend to make your code shorter, clearer, and easier to maintain.

If you can think of any types of duplication I've missed, please add a comment.

Posted by Anthony Williams
[/ design /] permanent link
Tags: , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

just::thread C++11 Thread Library V1.8.2 released

Tuesday, 06 November 2012

I am pleased to announce that version 1.8.2 of just::thread, our C++11 Thread Library has just been released.

This release adds support for gcc 4.7.2, and consequently official support for Ubuntu Quantal and Fedora 17.

just::thread is now available for the following compilers:

  • Microsoft Visual Studio 2005, 2008, 2010 and 2012 for both 32-bit and 64-bit Windows,
  • TDM gcc 4.5.2 and 4.6.1 for both 32-bit and 64-bit Windows,
  • g++ 4.3, 4.4, 4.5, 4.6 and 4.7 (4.7.2 or later) for both 32-bit and 64-bit Linux (x86/x86_64), and
  • MacPorts g++ 4.3, 4.4, 4.5, 4.6 and 4.7 (4.7.2 or later) for 32-bit and 64-bit MacOSX.

Get your copy of Just::Thread

Purchase your copy and get started with the C++11 thread library now.

As usual, existing customers are entitled to a free upgrade to V1.8.2 from all earlier versions.

Posted by Anthony Williams
[/ news /] permanent link
Tags: , , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

just::thread C++11 Thread Library V1.8.0 vs Microsoft Visual Studio 2012

Thursday, 06 September 2012

I am pleased to announce that version 1.8.0 of just::thread, our C++11 Thread Library has just been released.

This release adds official support for Microsoft Visual Studio 2012, as well as providing some minor bug fixes and improvements across the board.

Some people have asked how Just::Thread compares to the thread-library support in Microsoft Visual Studio 2012, given that VS2012 now provides the new C++11 concurrency headers, so I ran some of the Just::Thread tests against the VS2012 library. It turns out that there are quite a few places where Just::Thread has better conformance than VS2012, so if you're making heavy use of the C++11 thread library then upgrading to Just::Thread is an essential investment.

VS2012 thread library conformance issues

Here is a list of some of the areas where Just::Thread provides better conformance than VS2012. Some of these can be worked around; others are important for correctly-functioning code. This is just a sample, not a comprehensive list.

  • With the VS2012 library, you cannot use move-only types with std::promise, and std::async doesn't work with functions that return move-only types.
  • With the VS2012 library, std::thread doesn't work with move-only argument types.
  • With the VS2012 library, the wait_for and wait_until functions return incorrect values when used with a std::future that comes from a std::promise.
  • With the VS2012 library, when std::async is used with a launch policy of std::launch::async, the destructor of the returned std::future instance does not wait for the thread to complete.
  • With the VS2012 library, std::unique_lock does not check whether or not it owns the lock before calling operations on the underlying mutex, triggering undefined behaviour rather than throwing an exception in many cases.
  • With the VS2012 library, the std::atomic<> class template cannot be used on types without a default constructor.
  • With the VS2012 library, std::launch and other strongly-typed enums such as std::future_status are emulated with a namespace-scoped enum rather than a strongly-typed enum.

In all these cases (and more), Just::Thread conforms with the standard.

Just::Thread optimizations

Just::Thread also offers various optimizations over the VS2012 thread library such as the following.

  • The return value from a task run with std::async is copied/moved fewer times, and moved where possible.
  • A function object passed to std::thread is copied or moved fewer times.
  • The task passed to std::async is destroyed as soon as the task is completed, even if there are outstanding futures that reference the result.

Again, this is not a comprehensive list. Just::Thread has been carefully optimized to ensure common use cases have the best performance possible whilst remaining conformant to the C++11 standard.

Get your copy of Just::Thread

Purchase your copy and get started with the C++11 thread library now.

As usual, existing customers are entitled to a free upgrade to V1.8.0 from all earlier versions.

Posted by Anthony Williams
[/ news /] permanent link
Tags: , , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

ACCU 2012 Report

Thursday, 03 May 2012

I'm now back home again from ACCU 2012. As ever, it was an exhausting, but thoroughly enjoyable conference. With 5 tracks it was sometimes very hard to choose which session to attend.

There were a surprising number of talks on C, though mostly just to confirm that C is a dying language, a "zombie language" as Uncle Bob put it, even if there is still a niche or two where it is the best fit.

There was also a fair number of sessions on testing, especially TDD (even TDD in C), and a few sessions on other agile practices.

However, the big topic of the conference was C++11. Nicolai Josuttis ran a full day pre-conference tutorial on the C++11 library, and then there were sessions on the best and worst features of the C++11 library, C++11 for everybody, C++11 for the rest of us, C++11 allocators, writing generic code with C++11, the C++11 concurrency facilities, an interactive C++11 programming session, and even a C++11 pub quiz (with free beer!)

The other big topic was concurrency and parallelism, with 6 presentations, including two by me. Actors and Dataflow seemed to be a common theme — it looks like these ideas are finally filtering into the mainstream now that everyone and his dog has a multicore computer in their pocket. Not only did Russel, Jason and I reference them in our presentations, but several of the developers I spoke to had implemented their own actor or dataflow libraries, or were interested in using one.

My presentations

My own presentations went well. My first one (on Dataflow, Actors and High Level Structures in Concurrent Applications, on Thursday afternoon) was packed out. There were quite a few interesting questions from the audience, and someone told me it was the best presentation at the conference, which was really nice to hear. Several people asked about slides and the code samples, and I promised to make them available. The slides are here and the code samples here

My second presentation was the C++11 concurrency tutorial on Saturday morning. I was presenting in the first slot after the keynotes, and the speakers dinner was the night before, so a lot of people had been looking rather the worse for wear in the beginning. However, the room was almost full again, and the audience seemed to be awake — yet again there were many interesting questions that showed people were paying attention. The slides for are here and the code samples here

My book

My book was only published a couple of months before the conference, so it was exciting to see so many people clutching copies. Apparently the Blackwell's stall sold out, which was nice for me, but unfortunate for those that didn't manage to get a copy. If you wanted a copy then you can buy it direct from Manning, or from amazon.com, or amazon.co.uk.

Posted by Anthony Williams
[/ news /] permanent link
Tags: , , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

ACCU 2012

Wednesday, 25 April 2012

The main sessions of the ACCU 2012 conference start today.

This year I'm presenting two sessions. The first is tomorrow, at 4pm: Dataflow, actors, and high level structures in concurrent applications. In this session, I'll be talking about high level approaches for writing concurrent architectures including actors and dataflow architectures, and giving example code in multiple languages, including C++ and Groovy.

My second session is on Saturday at 11am, C++11 concurrency tutorial. This session will cover how to use the new C++11 thread library, as well as various guidelines for avoiding race conditions and deadlocks.

Hope to see you there!

Posted by Anthony Williams
[/ news /] permanent link
Tags:

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

C++ Concurrency in Action and Just::Thread Discounts

Wednesday, 25 April 2012

C++ Concurrency in Action cover

My book C++ Concurrency in Action was finally published on 29th February 2012, after 4 years of work. It's hard to believe that I can actually hold a copy in my hand; it's just been files on my computer for so long.

My book is a tutorial and reference to the thread library from the new C++11 standard. It also provides various guidelines for writing and testing multithreaded code, as well as sample implementations of thread-safe data structures and parallel algorithms

If you haven't already got a copy, you can order one direct from Manning, or from amazon.com, or amazon.co.uk. Alternatively, copies should be available at the ACCU 2012 conference in Oxford this week.

Discount on Just::Thread

If you have purchased the book then send a copy of your receipt or other proof of purchase to info@justsoftwaresolutions.co.uk for a 50% discount on a single user license of Just::Thread, our implementation of the C++11 thread library described in the book for Microsoft Visual Studio on Windows, and g++ on Windows, Linux and MacOSX.

Posted by Anthony Williams
[/ threading /] permanent link
Tags: , , ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

Happy New Year 2012

Tuesday, 10 January 2012

Now the post-holidays rush is over, I'd like to wish you all a Happy New Year!

2011 was another good year for me. Sales of Just::Thread, my implementation of the C++11 thread library continue to grow, and my book is finally in typesetting, and nearly ready for printing. We had hoped to get it done by the end of 2011, but last-minute corrections scuppered that. It should now be done relatively quickly.

It's also been a big year for the C++ community: C++11 is now an official ISO standard. This is the culmination of many years of hard work from a huge number of people, so it's good to see it finally done.

Popular articles

As is my custom, here's a list of the 10 most popular articles and blog entries from the Just Software Solutions website in 2011.

  1. Implementing a Thread-Safe Queue using Condition Variables
    A description of the issues around writing a thread-safe queue, with code.
  2. Implementing drop-down menus in pure CSS (no JavaScript)
    How to implement drop-down menus in CSS in a cross-browser fashion (with a teensy bit of JavaScript for IE).
  3. Importing an Existing Windows XP Installation into VirtualBox
    This article describes how I recovered the hard disk of a dead laptop to run as a VM under VirtualBox.
  4. Multithreading in C++0x part 1: Starting Threads
    This is the first part of my series on the new C++11 thread library. Links to the remaining parts are at the end of the article.
  5. Thread-Safe Copy and Move Constructors
    This is a guest post by Michael Spertus on writing copy and move constructors for objects with internal locks.
  6. Multithreading in C++0x part 2: Starting Threads with Function Objects and Arguments
    This is the second part of my series on the new C++11 thread library, which covers using callable objects when starting threads, and passing arguments to the thread function.
  7. Introduction to C++ Templates
    My basic introduction to C++ templates.
  8. Multithreading in C++0x part 8: Futures, Promises and Asynchronous Function Calls
    This is the eighth part of my series on the new C++11 thread library, which covers the "futures" mechanism for passing data between threads.
  9. Thread Interruption in the Boost Thread Library
    A description of the thread interruption feature of the Boost Thread library.
  10. Deadlock Detection with just::thread
    This article describes how to use the special deadlock-detection mode of our just::thread C++11 thread library to locate the cause of deadlocks.

What's coming in 2012?

Will 2012 be even better than 2011? I hope so. As I already mentioned, my book will finally be printed, which will be a big relief for me.

What are you looking forward to in 2011?

Posted by Anthony Williams
[/ news /] permanent link
Tags: ,

| Stumble It! stumbleupon logo | Submit to Reddit reddit logo | Submit to DZone dzone logo

Comment on this post

If you liked this post, why not subscribe to the RSS feed RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the right.

Older entries