Recently, I was tasked with writing a multi-threaded C++ sample application as a way of highlighting Intel’s multi-core processors. I decided to write a file compression program that compresses the contents of a specified directory branch. Once the branch to compress is specified, the processing can be broken down into 3 clearly defined sub tasks: file scrub and input, block compression, and output to the compressed file. I envisioned each of these tasks running in its own thread, with thread-safe queues to pass intermediate work between them. Of the code I had to write, the trickiest, I thought, would be the 2 queues.
Before I finished coding the application, a new requirement was added to the project. Intel has released a new C++ runtime library called “Threading Building Blocks.” I was asked to use this library in the sample application. So I downloaded an evaluation version of TBB from here, and headed back to the design board.
TBB comes with great documentation, including a “Getting Started” guide, a “Tutorial” that goes into more depth, and an exhaustive reference guide. I read the tutorial first, which gave me all I needed for my purposes. TBB offers support for several threading paradigms, including loop parallelization, task pipelining, thread-safe containers, and much more. The TBB pipeline was the best choice for what I was doing. I put each task in its own C++ class, registered the classes with the pipeline, and let it run. The best part was that I didn’t need to write the queues; TBB provided for passing the intermediate data between tasks.
I must say that dealing with the Threading Building Blocks was an unexpected pleasure. My experience using software tools for the first time has been a mixed bag at best; it’s often extremely frustrating to understand and utilize these tools quickly. For instance, the public-domain compression engine I originally intended to use cost me hours of configuration and building headaches, and I never did get it to build in a way that I could use. I ended up tossing it out and using a different compression engine altogether. TBB on the other hand was well documented, easy to understand and implement, and worked right out of the box. I must say that the next time I need to write a C++ application using multiple threads, I’ll likely reach for TBB.
The sample application, along with a readme file that does into the TBB code in some depth, can be found here.