[c++11]std::thread线程

学习网址

c++11中thread join和detach的区别
https://blog.csdn.net/c_base_jin/article/details/79233705

C++11 std::thread在类的成员函数中的使用
https://www.cnblogs.com/c4isr/p/9205164.html

C++多线程编程:基于现代C++的多线程库 - 知乎 (zhihu.com)

C++多线程编程  //这里讲的比较透彻
https://zhuanlan.zhihu.com/p/547312117

 C++ thread::hardware_concurrency 获取硬件支持并发数

 //获取获取硬件支持并发数
 int nums = thread::hardware_concurrency();
 std::cout<< "线程数量:" << nums<<std::endl;

 std::this_thread::sleep_for:  休眠函数

//休眠函数,windows可代替Sleep,linux可代替sleep
std::this_thread::sleep_for(std::chrono::seconds(1));

std::chrono::hours(1)         //1小时
std::chrono::minutes(1)       //1分钟
std::chrono::seconds(1)       //1秒
std::chrono::milliseconds(1)  //1毫秒

 std::thread的使用例子

#include <thread>
#include <iostream>

class demo {
  public:
      void p1() {
          std::cout << "i am p1" << std::endl;
      }

      void p2(const char *arg1, unsigned arg2) {
          std::cout << "i am p2 and my first arg is (" << arg1 << ") and second arg is (" << arg2 << ")" << std::endl;
      }

      //没有参数函数的例子
      std::thread Thread1() {
          return std::thread(&demo::p1, this);
      }

      //有参数的函数的例子
      std::thread Thread2(const char *arg1, unsigned arg2) {
          return std::thread(&demo::p2, this, arg1, arg2);
      }
};

int main() {

  demo *w = new demo();
  std::thread t = w->Thread1();
  t.join();

  w->Thread2("hello", 100).detach();
  return 0;

}

注意:

程序在退出之前,要等待线程结束

t.join() 这句是不能缺少的,如果缺了,在退出进程时会出现 Aborted(core dumped)

传类指针this给线程的例子 

 bool Client::create()
{

    m_pThread = new std::thread(Client::Thread, this);

    return true;
}
bool Client::Thread(void* pData)
{
    Client * pThread = (Client *)pData;

    if (pThread)
    {
        return pThread->ThreadRun();
    }
    return false;
}
bool Client::ThreadRun()
{
}

std::make_shared创建线程

void p3()
{
    std::cout << "i am p3" << std::endl;
}

int main()
{
// 如果是临时创建就用auto
auto thread_new = std::make_shared<std::thread>(p3);
// 如果是在类里声明,
//std::shared_ptr<std::thread> thread_new = std::make_shared<std::thread>(p3);

std::thread* pThread = thread_new.get();
pThread->join();
// thread_new->join();

return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,std::thread是用于创建和管理线程的类。以下是std::thread的基本用法: 1. 包含头文件:为了使用std::thread,需要包含头文件<thread>。 2. 创建线程:使用std::thread的构造函数创建线程对象,并将函数指针或可调用对象作为参数传递给构造函数。例如: ```cpp void myFunction() { // 线程执行的代码 } std::thread myThread(myFunction); // 创建一个新线程,执行myFunction ``` 3. 启动线程:通过调用线程对象的成员函数`std::thread::join()`来启动线程。例如: ```cpp myThread.join(); // 启动线程执行 ``` 4. 传递参数:如果需要向线程传递参数,可以在创建线程时将参数作为额外的参数传递给构造函数。例如: ```cpp void myFunction(int arg1, std::string arg2) { // 使用传递的参数执行操作 } std::thread myThread(myFunction, 42, "Hello"); // 创建线程并传递参数 ``` 5. 分离线程:通过调用线程对象的成员函数`std::thread::detach()`可以将线程分离,使其在后台运行而不需要等待。例如: ```cpp myThread.detach(); // 分离线程,使其在后台运行 ``` 6. 获取线程ID:可以通过`std::thread::get_id()`成员函数获取线程的唯一标识符。例如: ```cpp std::thread::id threadId = myThread.get_id(); // 获取线程的ID ``` 7. 线程同步:在线程间进行同步操作时,可以使用互斥锁(std::mutex)和条件变量(std::condition_variable)等同步原语来确保线程安全。 注意事项: 线程的生命周期应与其所执行的函数的生命周期相匹配,以避免悬挂引用或访问已销毁的对象。 - 分离线程后,不能再对其进行join()操作,也无法获取其状态或等待其完成。 这是std::thread的基本用法,您可以根据实际需求进一步了解和使用其他相关函数和特性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值