程序员求职经验分享与学习资料整理平台

网站首页 > 文章精选 正文

[编程基础] C++多线程入门1-创建线程的三种不同方式

balukai 2025-01-02 14:37:31 文章精选 6 ℃


原始C++标准仅支持单线程编程。新的C++标准(称为C++11或C++0x)于2011年发布。在C++11中,引入了新的线程库。因此运行本文程序需要C++至少符合C++11标准。

文章目录

  • 1 创建线程的三种不同方式
  • 1.1 创建线程
  • 1.2 区分线程
  • 1.3 参考

1 创建线程的三种不同方式

在本章中,我们将讨论如何使用std::thread在C++11中创建线程。
在每个C++应用程序中,都有一个默认的主线程,即main()函数。在C++11中,我们可以通过创建std::thread类的对象来创建其他线程。每个std::thread对象都可以与一个线程关联。因此我们需要引入头文件为:

#include <thread>

那么std::thread在构造函数中接受什么?我们可以在std::thread对象上附加一个回调,该回调将在新线程启动时执行。这些回调可以是:

  1. 函数指针
  2. 函数对象
  3. Lambda函数

1.1 创建线程

可以这样创建线程对象:

std::thread thObj(<CALLBACK>);

新线程将在创建新对象后立即启动,并将与启动该线程的线程并行执行传递的回调。而且,任何线程都可以通过在该线程的对象上调用join()函数来等待另一个线程退出。
让我们看一个示例,其中主线程将创建一个单独的线程。创建此新线程后,主线程将在控制台上打印一些数据,然后等待新创建的线程退出。我们使用三种不同的回调机制来实现上述功能。

  1. 使用函数指针创建线程
#include <thread>
#include <iostream>

using namespace std;

void thread_function()
{
  for (int i = 0; i < 10000; i++);
  std::cout << "thread function Executing" << std::endl;
}

int main()
{
  // 创建线程 
  std::thread threadObj(thread_function);
  for (int i = 0; i < 10000; i++);
  std::cout << "Display From MainThread" << std::endl;
  // 等待线程的结束
  threadObj.join();
  std::cout << "Exit of Main function" << std::endl;
  return 0;
}

输出为:

Display From MainThreadthread function Executing

Exit of Main function
  1. 使用函数对象创建线程
#include <iostream>
#include <thread>

class DisplayThread
{
public:
  void operator()()
  {
    for (int i = 0; i < 10000; i++);
    std::cout << "Display Thread Executing" << std::endl;
  }
};

int main()
{
  std::thread threadObj((DisplayThread()));
  for (int i = 0; i < 10000; i++);
  std::cout << "Display From Main Thread " << std::endl;
  std::cout << "Waiting For Thread to complete" << std::endl;
  threadObj.join();
  std::cout << "Exiting from Main Thread" << std::endl;
  return 0;
}

输出为:

Display Thread ExecutingDisplay From Main Thread
Waiting For Thread to complete

Exiting from Main Thread
  1. 使用Lambda函数创建线程
#include <iostream>
#include <thread>

int main()
{
  int x = 9;
  std::thread threadObj([] {
    for (int i = 0; i < 10000; i++)
      std::cout << "Display Thread Executing" << std::endl;
  });

  for (int i = 0; i < 10000; i++)
    std::cout << "Display From Main Thread" << std::endl;

  threadObj.join();
  std::cout << "Exiting from Main Thread" << std::endl;
  return 0;
}

输出为:

Display Thread ExecutingDisplay From Main Thread

Exiting from Main Thread

1.2 区分线程

每个std::thread对象都有一个关联的ID,我们可以使用成员函数来获取,给出关联的thread对象的ID。

std::thread::get_id()

要获取当前线程使用的标识符,即

std::this_thread::get_id()

如果std::thread对象没有关联的线程,则get_id()将返回默认构造的std::thread::id对象,即“没有任何线程”。std::thread::id是一个Object,也可以在控制台上进行比较和打印。让我们看一个例子。

#include <iostream>
#include <thread>

void thread_function()
{
  std::cout << "Inside Thread::ID  = " << std::this_thread::get_id() << std::endl;
}
int main()
{
  std::thread threadObj1(thread_function);
  std::thread threadObj2(thread_function);

  if (threadObj1.get_id() != threadObj2.get_id())
    std::cout << "Both Threads have different IDs" << std::endl;

  std::cout << "From Main Thread::ID of Thread 1 = " << threadObj1.get_id() << std::endl;
  std::cout << "From Main Thread::ID of Thread 2 = " << threadObj2.get_id() << std::endl;

  threadObj1.join();
  threadObj2.join();
  return 0;
}

输出为:

Inside Thread::ID? = 14756
Inside Thread::ID? = 15500
Both Threads have different IDs
From Main Thread::ID of Thread 1 = 14756
From Main Thread::ID of Thread 2 = 15500

1.3 参考

?https://thispointer.com//c-11-multithreading-part-1-three-different-ways-to-create-threads/??

Tags:

最近发表
标签列表