In concurrent computing, the producer-consumer problem is a classical question. We often use it to demonstrate different synchronization technics. In a previous post, we solved it with mutexes. Today, we will revisit the problem and improve the solution with condition variables.

This tutorial consists of three parts. First, we will review the mutexes only answer and analyze its drawback. Next, we will introduce condition variables. Finally, we will develop a better solution with what we learn.

Producer-Consumer with Mutex

If you haven’t read my previous post, I strongly recommended you check it first. Here is the core functionality:

The inefficiency comes from…

The producer-consumer problem is a classical multi-threaded synchronization problem in concurrent programming. Today, we will try to address it in C++ with mutexes.

This tutorial consists of three parts. First, we define and analyze the challenging part of the quiz. In the second section, we explain and learn to use mutexes in C++. Finally, we apply mutexes to accomplish our solution.

Problem Statement

The producer-consumer problem is a set of problems with lots of variants. In this tutorial, we focus on the simplest version:

  1. The system has one producer thread and one consumer thread.
  2. The size of the buffer between producer and…

When building a complicated C++ application, the process can involve multiple tasks including pre-processing, compilation, linking, library generation, etc. Most of the time, for time-saving purposes, we only want to re-run certain steps but not all to rebuild the application reflecting an incremental change. It’s hard to bookkeep what’s changed and what’s not manually. Build automation tools can help organize such complicated processes.

In this tutorial, we will use one popular tool for C++ — Make to demonstrate how to automate the building process.

1. What is Make — an Important Legacy Tool

Surprisingly, make (1976) has a longer history than C++ language (1978). It was designed to be…

In the previous article of this serials, we introduced the basics concepts in a version control system (VCS) — repository, commit and branch. We know code change is recorded as a commit. The next thing is to make a commit.

That sounds like an easy job — modify the file and update the repository, right? Well, it is, but in practice, there are places you can easily shoot yourself in the foot.


So, where is the pitfall? To put it simply, not every change on your local files is expected to be committed. You may generate temporary files during your…

Version control systems (VCS), also known as SCM (Source Code Management), are tools to help software development teams to manage changes to source code over time. It is the one tool every software developer must deal with in their daily work.

This series is written to help beginners getting started with VCS (which can be hard!). While I am not going to focus on specific tool’s usage, whenever needed, I will use terminology from git. If your company uses a different tool, don’t worry, the concept is similar. …

Move semantics and rvalue references are two advanced and confusing features added in C++11. In this article, I will explain what they are and why they are needed. Let’s start with the main problem they are trying to tackle (yes, together).

Problem: Unnecessary Copy of Objects

C++’s major advantage compared to other programming languages is it’s fast. However, there had been one problem to slow down C++ programs before C++11: unnecessary copy of objects. Take a look at the following example, which create and return an integer array:

Before C++11, the above code will perform terribly due to the copying of array ret. …

In this tutorial, we will talk about libraries in C++ and how to create/use them. A library is a collection of pre-compiled code that can be re-used by programs. There are 2 types of libraries: static library and dynamic library.

1. Static Library vs Dynamic Library

A static library (or archive) contains code that is linked to users’ programs at compile time. The executable file generated keeps its own copy of the library code.

A dynamic library (or shared library) contains code designed to be shared by multiple programs. The content in the library is loaded to memory at runtime.

In the last tutorial, we talked about the basic compilations steps (preprocessing, compilation, linking) in the C++ and two important concepts “forward declaration” and “one definition rule”. If you haven’t read it, I strongly recommend reading it first.

In this tutorial, we dive deep into one place where people made a lot of mistakes — header files. I have seen product code in large companies that didn’t have header files properly written and caused issues for maintenance. It definitely deserves more attention. Let’s start.

1. Why header files?

Header files provide a convenient way to put forward declarations from other files. In C++, every…

In the last tutorial, we compiled a simple single source file C++ program. However, most programs consist of multiple source files. It’s also good practice to separate source code into multiple files for better organization. From now on, we are starting to handle multiple file compilation and all the common pitfalls around it.

Specifically, this one focuses on the compiling process. A C++ compilation process can be divided into 3 stages:

  1. Preprocessing.
  2. Compilation.
  3. Linking.

In each stage, we only pick the part relevant to compiling multiple files. Let’s go through them one by one.

1. Preprocessing

During preprocessing, the preprocessor will deal…

This is a series of tutorials to help you start on C++ development (in Linux/macOS) and get familiar with useful tools. It aims to help people who have basic knowledge of C++ language and want to know more about the C++ compilation/debugging/development process in general. The tutorial will mostly focus on how to use tools (compiler, debugger, linker, profiler, etc. ). I will only explain C++ language when necessary.

In the first tutorial, we introduce GCC, the C++ compiler. You will learn how to compile a simple program with it and some commonly used command-line options.

1. What is GCC?

GCC (GNU Compiler Collection)…

Building Compilers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store