Bits by Gavin Baker

C++11 Futures

Concurrency is one of the most significant challenges facing software development today. As the gains in processor performance diminish year over year, additional cores have become the norm. For some years now, multicore processors have become the norm.

Taking advantage of multiple cores has usually required writing multithreaded code, which can be complex to design and debug. One of my favourite new features in the C++11 standard library is the future module. This provides an extra layer of abstraction over threads, providing a simple mechanism for asynchronous (ie. concurrent) processing.

C++11 Smart Pointers: Shared Pointer

In the last article on smart pointers, we looked at std::unique_ptr, which provides a simple and safe smart pointer to wrap heap allocations. As the name implies, this smart pointer type cannot be shared between multiple threads.

So then how can you ensure that the memory is freed once all referring threads have finished with the resource? This is especially difficult when the thread lifecycle is non-deterministic.

What Is Rust?

Rust is a compiled, hybrid imperative/object- oriented/functional language. It appeals directly to any C++ developer who has battled with memory management, and Python developers who long for faster code. So why might you be interested in learning Rust?

Threading With Boost - Part III: Mutexes

In Part I of this series on Boost threading, we looked at the basics of how to create and run threads using the Boost libraries. Then we reviewed the main issues encountered with multithreading code in Part II: Threading Challenges. One of the biggest challenges is safely managing concurrent access to a resource. A Mutex provides a way to serialise access to a shared resource, such that only one thread is accessing the data at any given time, to ensure your data is consistent. In this article, we look at how to create and use Boost mutexes.

Bug of the Year - 2011

My favourite bug of all time was uncovered because I was just too impatient.

I was working on a fairly large and complex embedded project. A microcontroller was interfacing with some custom hardware. It would configure, control, and monitor the hardware. The microprocessor was running an RTOS with a lightweight TCP/IP stack, so remote clients could connect to the device via Ethernet and perform remote configuration. We also used this for diagnostics and testing during development. We had successfully used a similar design on several products, with great results. We had also designed a protocol for monitoring and control that was used in earlier models, and we extended it here – though this product was significantly larger and more complex than its predecessors. What could possibly go wrong?

Good Code Gardening

During a code review at work recently, we had an interesting discussion about code maintenance. You could say that coding is a bit like gardening: while you are planting new seedlings, do you weed nearby areas as you go, or save up all the weeding for the next sunny weekend? Should code maintenance be a continual, gradual process, or does it warrant being scheduled as a task in its own right?

25 Tips for Intermediate Mercurial Users

I recently read an interesting article by Andy Jeffries entitled 25 Tips for Intermediate Git Users (linked to via proggit) . It had lots of useful information condensed into bite-sized task-oriented chunks.

I’ve been using Mercurial for a while now, so I thought I would write a similar set of tips by translating from git to the equivalent hg commands. Thanks to Andy for blessing this translation work. There may well be some mistakes herein – please leave a comment if you have any improvements or fixes to suggest.

Threading With Boost - Part II: Threading Challenges

In Part I of this series on Boost, we looked at the basics of how to create and run threads using the Boost libraries. But once you have more than one thread running in a process, you have to deal with the problems and challenges that threads can introduce. So, before delving into the mechanics of how to use mutexes and other threading constructs, we look at what can go wrong – and how to avoid it.

STL: Filtering

The STL makes it easy to create lists, iterate over lists, and apply a function to each member of a list. So how do you filter a vector according to some criteria? It’s not hard, but the obvious solution isn’t quite enough. Here’s how.