📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page

Was this helpful?

  1. Chapter 12: Multithreading

Atomic Operations

In previous chapter, we wrote a multithreaded program that sells bus tickets. We used mutual exclusion for thread synchronization. Inside the critical section, only one thread can execute at the same time. Though mutex is the most common tool for synchronization, it is somehow heavy since it block other threads to keep waiting. For some simple operations, for example adding and subtracting, we can keep thread safety without using any locks. These operations are known as atomic operations.

The word "atomic" means uncuttable. A task performed by a computer can't be broken into smaller steps if it is atomic. In other words, there is no way for a thread to slip through an atomic operation concurrently performed by another one. Remember in traditional ++ operation, it takes three steps to add a variable by 1: load the value of the variable into a register, add the register by 1, and store the value back to the variable. With atomic operations, the load and store operations of the variable are uncuttable, therefore there's no possibility of race condition.

mov eax, a
add eax, 1
mov a, eax

Atomic operations are low-level, which need the support of hardware. The most common strategy to achieve atomicity is called compare-and-swap (CAS). It compares the contents of a memory location with a given value and, only if they are the same, modifies the contents of that memory location to a new given value.

C++ 11 provides an atomic library which supports atomic operations:

#include <atomic>

Here we use a simple example to illustrate it. We defined two variables, isReady and myCount which is of type atomic_bool and atomic_int respectively. They are just aliases for atomic<bool> and atomic<int>. Then we create 10 threads, and each of them call myCount++ for a hundred times. this_thread::yield() is called to skip the current CPU time slice and wait for the next scheduling, if the thread creation has not finished yet. If we are not using atomic operations here, there would be races between threads, and the final result is uncertain. But with atomic operations, we can get a correct result of 1000.

atomic_bool isReady;
atomic_int myCount;
​
void task() {
    while (!isReady) {
        this_thread::yield();
    }
    for (int i = 0; i < 100; i++) {
        myCount++;
    }
}
​
int main() {
    list<thread> tlist;
    for (int i = 0; i < 10; i++) {
        tlist.push_back(thread(task));
    }
    isReady = true;
    for (thread &t : tlist) {
        t.join();
    }
    cout << myCount << endl;    // 1000
    return 0;
}

However, the above program still has potential problems. We will discuss more in the next article.

PreviousProducer-Consumer ProblemNextThread Visibility and volatile

Last updated 4 years ago

Was this helpful?