📖
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 11: Function Objects and Binders

std::bind(): A Simple Thread Pool

In previous chapter, we have introduced bind1st and bind2nd. These binders are used to bind function parameters with fixed value. However, they only apply to functions with two parameters. Now let's introduce a more powerful binder in C++ 11: std::bind().

std::bind() binds the parameters in a function, and returns a function object. std::bind() is implemented with function templates, so it supports type deduction of parameter types, and various number of parameters. std::bind() can be used to bind functions with different number of parameters, and member functions as well:

void foo(string str) {
    cout << str << endl;
}
​
int sum(int a, int b) {
    return a + b;
}
​
class Test {
public:
    int sum(int a, int b) {
        return a + b;
    }
};
​
int main() {
    bind(foo, "foo")(); // foo
    cout << bind(sum, 10, 20)() << endl;    // 30
    cout << bind(&Test::sum, Test(), 20, 30)() << endl; // 50
    return 0;
}

In the previous example, all parameters are bound with std::bind(). We can also bind a part of them with parameter placeholders. Parameter placeholders are within the namespace of placeholders, and consist of a underline and a number, which indicates the order of parameters. In the following example, we put a placeholder at the first parameter of sum(), and bind the other one with 10. It is the same as using bind1st(sum, 10), but is much more flexible.

using namespace placeholders
int main() {
    cout << bind(sum, _1, _2)(20, 30) << endl;  // 50
    cout << bind(sum, _1, 10)(20) << endl;  // 30
    return 0;
}

Since std::bind() returns a function object, we can use a std::function object with it. The next example use an object of type function<void(string)> to receive the returned function object, where the binder can be reused later.

int main() {
    function<void(string)> f = bind(foo, _1);
    f("foo");   // foo
    f("bar");   // bar
    return 0;
}

std::bind() are widely used in large-scale C++ projects. We can implement a simple thread pool with the feature of std::bind(). More about multithreading will be introduced later. Now let's just look at this example.

#include <functional>
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
​
class Thread {
 public:
  Thread(function<void()> func) : _func(func) {}
​
  thread start() {
    thread t(_func);
    return t;
  }
​
 private:
  function<void()> _func;
};
​
class ThreadPool {
 public:
  ThreadPool() {}
​
  ~ThreadPool() {
    for (int i = 0; i < _pool.size(); i++) {
      delete _pool[i];
    }
  }
  void startPool(int size) {
    for (int i = 0; i < size; i++) {
      _pool.push_back(new Thread(bind(&ThreadPool::runInThread, this, i)));
    }
​
    for (int i = 0; i < size; i++) {
      _handler.push_back(_pool[i]->start());
    }
​
    for (thread &t : _handler) {
      t.join();
    }
  }
​
 private:
  vector<Thread *> _pool;
  vector<thread> _handler;
  void runInThread(int id) { cout << "Thread " << id << endl; }
};

The above example has a Thread class which basically takes a function object and start() executes that function in the thread. Then inside ThreadPool, we create a certain number of Thread objects, and call their start() method respectively. Here we also have a function runInThread() which is exactly the function we want to execute in each thread. Since this function is a member function, we can not pass it as a function pointer or else to the constructor of Thread. Instead, we use std::bind() to bind its parameter, and convert it into a function object. In this way, Thread::start() can call the function directly with operator ().

Now in the main function we create a thread pool, and start with 10 threads.

int main() {
  ThreadPool pool;
  pool.startPool(10);
  return 0;
}

The program has the following output. Since each print statement is executed in each thread disorderly, the output is disordered as well.

Thread Thread 10
​
Thread 2
Thread 3
Thread 4
Thread 5
Thread 6
Thread 7
Thread 8
Thread 9
PreviousMore about std::functionNextMore about Lambda Expressions

Last updated 4 years ago

Was this helpful?