C++ concurrent programming (1): Create threads

The original text is reproduced at: https://segmentfault.com/a/1190000006232497?utm_source=sf-related

This series is a summary of my recent study of C++ concurrent programming. The article and code were originally based on Boost.Thread, but more and more recently, it has been discovered that the built-in thread and synchronization tools of STL are sufficiently perfect.

STL and Boost threads are very similar in design and usage. Once you have mastered one, it is not difficult to switch to the other. If you have to compare, Boost is more complete, for example, Boost provides thread_group and upgrade_lock, STL does not.

This section introduces "Thread Creation".

Hello 1

Create a thread through a function without parameters.

#include <iostream>
#include <thread>

void Hello() {
  // 睡眠一秒以模拟数据处理。
  std::this_thread::sleep_for(std::chrono::seconds(1));
  std::cout << "Hello, World!" << std::endl;
}

int main() {
  // 创建一个线程对象,注意函数 Hello 将立即运行。
  std::thread t(&Hello);

  // 等待线程结束。
  // 否则线程还没执行(完),主程序就已经结束了。
  t.join();

  return 0;
}

Hello 2

Create a thread through a function with parameters.

#include <iostream>
#include <thread>

void Hello(const char* what) {
  // 睡眠一秒以模拟数据处理。
  std::this_thread::sleep_for(std::chrono::seconds(1));
  std::cout << "Hello, " << what << "!" << std::endl;
}

int main() {
  std::thread t(&Hello, "World");
  
  // 等价于使用 bind:
  //   std::thread t(std::bind(&Hello, "World"));

  t.join();

  return 0;
}

Hello 3

A thread is created through a function object-that is, a functor.

#include <iostream>
#include <thread>

class Hello {
public:
  void operator()(const char* what) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Hello, " << what << "!" << std::endl;
  }
};

int main() {
  Hello hello;

  // 方式一:拷贝函数对象。
  std::thread t1(hello, "World");
  t1.join();

  // 方式二:不拷贝函数对象,通过 boost::ref 传入引用。
  // 用户必须保证被线程引用的函数对象,拥有超出线程的生命期。
  // 比如这里通过 join 线程保证了这一点。 
  std::thread t2(std::ref(hello), "World");
  t2.

  return 0;
}

Hello 4

Create a thread through a member function.
The difference from the previous example is that you need to bind this pointer with bind as the first parameter.

#include <iostream>
#include <thread>

class Hello {
public:
  Hello() {
    std::thread t(std::bind(&Hello::Entry, this, "World"));
    t.join();
  }

private:
  // 线程函数
  void Entry(const char* what) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Hello, " << what << "!" << std::endl;
  }
};

int main() {
  Hello hello;
  return 0;
}

Counter

Create two threads and count backwards.
This example incidentally demonstrates that the detached thread, the detached thread, fends for itself, is not controlled, and can no longer join.

#include <iostream>
#include <thread>

class Counter {
public:
  Counter(int value) : value_(value) {
  }

  void operator()() {
    while (value_ > 0) {
      std::cout << value_ << " ";
      --value_;
      std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    std::cout << std::endl;
  }

private:
  int value_;
};

int main() {
  std::thread t1(Counter(3));
  t1.join();

  std::thread t2(Counter(3));
  t2.detach();

  // 等待几秒,不然 t2 根本没机会执行。
  std::this_thread::sleep_for(std::chrono::seconds(4));
  
  return 0;
}

Video explanation of timer design for massive timing tasks in multithreaded environment

Guess you like

Origin blog.csdn.net/qq_28581269/article/details/112911946