使用 std::thread 创建线程

背景

运行一个可执行程序会产生成一个进程,同时该进程所属的主线程开始自动运行;主线程从 main() 函数开始执行,当 main() 函数返回,则整个进程执行完毕。
当我们创建自己的线程时,也需要从一个函数开始执行;若该函数执行完毕,则线程结束。

std::thread

在 C++11中新引入了线程库 std::thread ,用于创建线程,包含在头文件 #include 中 。
①.std::thread 对象只具有移动属性,不具备拷贝属性,其拷贝构造、拷贝赋值函数是 delete 的。
②.每个线程都具有唯一的 id,使用数字表示。可使用函数 std::this_thread::get_id() 来获取当前线程的 id,也可以通过 std::thread 对象的成员函数 get_id() 来获取。

代码示例

本质上创建 std::thread对象需要传入一个可调用对象,可以由以下几种方式:
①.使用普通函数创建线程

#include "thread"
#include "iostream"
using namespace std;
void functionToThread()
{
  cout << "线程启动......" << endl;

  cout << "线程结束......" << endl;
}
int main()
{
  thread myThread(functionToThread);

  system("pause");
   return 0;
}

②.使用 lambda 表达式创建线程

#include "thread"
#include "iostream"
using namespace std;
int main()
{
  auto lambdaThread = [](){
    cout << "线程启动......" << endl;
    cout << "线程结束......" << endl;
  };

  thread myThread(lambdaThread);

  system("pause");
   return 0;
}

③.使用类对象创建线程

#include "thread"
#include "iostream"
using namespace std;
class classDemo
{
public:
  void operator()()
{
    cout << "线程启动......" << endl;
    cout << "线程结束......" << endl;
  }
};
int main()
{
  classDemo demo;
  thread myThread(demo);

  system("pause");
   return 0;
}

④.使用类的成员函数创建线程

#include "thread"
#include "iostream"
using namespace std;
class classDemo
{
public:
  void functionToThread()
{
    cout << "线程启动......" << endl;
    cout << "线程结束......" << endl;
  }
};

int main()
{
  classDemo demo;
  thread myThread(&classDemo::functionToThread,&demo);
  //使用bind
  thread mymyThread1(bind(&classDemo::functionToThread, &demo));
  //使用mem_fn
  thread mymyThread2(mem_fn(&classDemo::functionToThread), &demo);

  system("pause");
   return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用std::thread可以创建线程。可以通过以下几种方式来创建线程: 1. 使用普通函数创建线程:通过将函数名作为参数传递给std::thread的构造函数来创建线程。例如,可以定义一个普通函数functionToThread,并将其作为参数传递给std::thread的构造函数。 2. 使用lambda表达式创建线程:可以使用lambda表达式来定义线程的执行代码,并将其作为参数传递给std::thread的构造函数。 注意事项: - std::thread对象只具有移动属性,不具备拷贝属性。 - 每个线程都具有唯一的id,可以使用std::this_thread::get_id()函数来获取当前线程的id,也可以通过std::thread对象的成员函数get_id()来获取。 示例代码如下: #include <iostream> #include <thread> using namespace std; void functionToThread() { cout << "线程启动......" << endl; cout << "线程结束......" << endl; } int main() { // 使用普通函数创建线thread myThread(functionToThread); // 使用lambda表达式创建线程 auto lambdaThread = [](){ cout << "线程启动......" << endl; cout << "线程结束......" << endl; }; thread myThread2(lambdaThread); // 等待线程执行完毕 myThread.join(); myThread2.join(); return 0; } <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [使用 std::thread 创建线程](https://blog.csdn.net/lizhichao410/article/details/123547768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值