首页 > 其他分享 >std::thread 一:创建线程的三种方式

std::thread 一:创建线程的三种方式

时间:2023-06-18 23:14:27浏览次数:31  
标签:std cout thread 创建 线程 include

前言:

#include <thread>

thread.join()      // 阻塞
thread.detach()    // 非阻塞
thread.joinable()  // bool,判断线程是否支持join或者detach

 

正文:

创建线程有三种方式,分别是:使用函数来创建线程、使用自定义的类来创建线程、使用lambda函数来创建线程

 

一、使用函数来创建线程

void func1()
{
    cout << "我是不带参数的函数" << endl;
}

void func2(int num) 
{ 
    cout << "我是带参数的函数,参数是:" << num << endl;
}

int main()
{
    cout << "thread begin" << endl;

    thread t1(func1);
    t1.join();

    thread t2(func2, 10);
    t2.join();

    cout << "thread end" << endl;
    
    return 0;
}

 

二、使用自定义的类来创建线程

#include <iostream>
#include <thread>
using namespace std;

class myClass1
{
public:
    // 重写operator方法
    void operator()()
    {
        cout << "我是不带参数的类" << endl;
    }
};

class myClass2
{
public:
    int m_i;
    myClass2(int i) :m_i(i) {};
    void operator()()
    {
        cout << "我是带参数的类,参数是:" << m_i << endl;
    }
};

int main()
{
    cout << "thread begin" << endl;

    myClass1 c1;
    thread t1(c1);
    t1.join();

    myClass2 c2(6);
    thread t2(c2);
    t2.join();

    cout << "thread end" << endl;
    return 0;
}

 

 

三、使用lambda表达式来创建线程

#include <iostream>
#include <thread>
using namespace std;

int main()
{
    cout << "thread begin" << endl;

    auto myLambda1 = [] {
        cout << "卡布达  卡布达 我是不带参数的 lambda" << endl;
    };

    auto myLambda2 = [](int num) {
        cout << "卡布达  卡布达 我是带参数的lambda,参数是:" << num << endl;
    };

    thread t1(myLambda1);
    t1.join();

    thread t2(myLambda2, 666);
    t2.join();

    cout << "thread end" << endl;

    return 0;
}

 

标签:std,cout,thread,创建,线程,include
From: https://www.cnblogs.com/shiyixirui/p/17489946.html

相关文章

  • std::thread 二:互斥量(lock() & unlock())
     mutex 互斥量的作用是保护共享数据*:有lock() 就一定要有 unlock()#include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;i<10000;i++)......
  • std::thread 二:互斥量(lock_guard())
    *:使用lock_guard后,就不可以使用lock()和unlock()*:lock_guard和智能指针一样,会自动解锁 #include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;......
  • std::thread 二:互斥量(多个互斥量的解决方法)
     //*:这里的lock是函数模板,最少传两个互斥量//第一种,使用lock和unlockstd::mutexm_mutex1;std::mutexm_mutex2;std::lock(m_mutex1,m_mutex2);m_mutex1.unlock();m_mutex2.unlock();//第二种,使用lock和lock_guardstd::mutexm_mutex1;std::mutexm_m......
  • std::thread 二:互斥量(带超时的互斥量 timed_mutex())
     timed_mutex、 try_lock_for、 try_lock_until #include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;i<10000;i++)......
  • std::string 拼接字符串
      #include<iostream>#include<string>#include<sstream>usingnamespacestd;intmain(){stringa="123";stringstreamb;b<<123<<"456"<<789<<"-=-=";......
  • Java多线程-Lesson01-线程的创建
    线程创建的三种方式继承Thread类步骤:继承Thread类重写run()方法调用start()开启线程重写run()方法:@Overridepublicvoidrun(){for(inti=0;i<200;i++){System.out.println("run():"+i);}} run()方法里面就是我们多......
  • C++面试八股文:std::string是如何实现的?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第18面:面试官:std::string用过吧?二师兄:当然用过(废话,C++程序员就没有没用过std::string的)。面试官:std::string("hello")+"world"、"hello"+std::string("world")和std::string("hello")+std::string("world")的......
  • C++面试八股文:std::string是如何实现的?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第18面:面试官:std::string用过吧?二师兄:当然用过(废话,C++程序员就没有没用过std::string的)。面试官:std::string("hello")+"world"、"hello"+std::string("world")和std::string("hello")+std::string("world")的......
  • 重拾线程池状态5种状态
    线程池状态分类线程的状态具有运行与关闭的状态,那么线程池也不例外。java线程池具有五种状态。RUNNINGSHUTDOWNSTOP TIDYINGTERMINATED见源码 ThreadPoolExecutor类种的属性//runStateisstoredinthehigh-orderbitsprivatestaticfinalintRUNNIN......
  • 一篇搞定守护线程和非守护线程的区别
    需求:如果想让某个线程随着主线程的结束而结束,该如何做?例如线程a如何随着主线程的结束而结束,解决这个问题我们就要应用线程的守护线程(后台线程),这样线程就会随着主线程的结束而结束。在Java中,可以创建两种线程守护线程守护线程 就是大家常说的DaemonThread线程也叫......