Numărul 82
Abonament PDF

C++ concurrency - Interview with Anthony Williams

Ovidiu Mățan
Fondator @ Today Software Magazine

We are talking with Anthony Williams, the author of Concurrency in Action, now on Second Edition. He's passion is multithreading and for several years he was the primary developer of Boost Thread Library in C++.

You have in your book an interesting clarification / definition of the difference between parallelism and concurrency. Could you please explain this to our readers?

Anthony Williams: The difference between parallelism and concurrency is primarily a difference in focus.

When thinking about parallelism, then the focus is about making best use of the hardware to do the processing. You don't want extra threads running, because that would cause excessive switching, and you want to ensure all your cores have meaningful work to do at all times.

When thinking about concurrency, then you have a set of tasks which could potentially run at the same time, because they are mostly independent, and you are using threads to separate concerns so you don't have to handle the task switching yourself. e.g. Background spell checking. In this scenario, you might use multiple threads even on a single core machine, in order to simplify your design.

How do you prefer to approach concurrency, within multiple processes or multiple threads? Can we consider some situations where one of them is a better approach?

Anthony Williams: I usually prefer to use multiple threads rather than processes, because the communication overhead is lower: you can pass data around just by passing a pointer rather than with an expensive interprocess communication mechanism.

That said, multiple processes can be more scalable, as you can more easily run the processes on separate machines for a larger-scale network parallel computation. The enforced isolation can also lead to a safer architecture, as there is no mutable shared state.

Do you have some advices for developers to avoid deadlocks? What about mechanisms to find any potential deadlocks in the existing code?

Anthony Williams: Do not call callbacks or other external code while holding a lock.

Pay attention to the order of locks, hold locks for as short a time as possible.

Use something like thread sanitizer to detect problems.

How do you see functional programming style in C++ and do you think there is a better approach?

Anthony Williams: C++ is a multi-paradigm language, and functional style is one way to

write code. In some circumstances it is highly effective. Certainly, it helps avoid problems with shared mutable state while writing multithreaded code.

Sometimes code written in functional style is slower, or ends up more convoluted, so another style works better. It's always a trade-off.

What thread pool implementation do you regular use?

Anthony Williams: I don't have one implementation I always use. I have one for my own code which is under continual development, and I also use whatever is used by my clients: TBB, Windows thread pool, etc.

Tell us few highlights of your presentation at The Developers 2019 conference?

Anthony Williams: One of the best things that will be added to C++20 is the stop_source/stop_token/jthread package.

stop_source/stop_token make cooperative cancellation easier to manage by providing a clear signal path. jthread then integrates that into a thread wrapper, so you can easily request that the thread stop, and the thread can easily check if stopping has been requested. jthread also provides automatic request-stop-and-join on destruction, making it a "proper" RAII type. It is a better replacement for std::thread.

I also really like the new latch type. I use it all the time for testing multithreaded code.

I will also talk about coroutines, and executors, and plenty more.



  • Accenture
  • Bosch
  • ntt data
  • Betfair
  • FlowTraders
  • MHP
  • Connatix
  • BoatyardX
  • metro.digital
  • Colors in projects