首页 > 其他分享 >创建线程有多少种方式

创建线程有多少种方式

时间:2023-03-01 16:36:35浏览次数:21  
标签:std main cout thread int 创建 线程 include 多少

C++多线程

C++11引入了线程类thread,头文件为
#include <thread>
创建多线程的方法:
std::thread threadName(functionName, parameter1, paramater2, …);
传递参数可以传左值,右值,引用(使用std::ref)和常引用(使用std::cref)。
例如:
std::thread threadName(functionName, leftValue, rightValue, std::ref(refArg), std::cref(constRefArg));
参数先copy或move到std::thread对象后再move给函数。

多线程的创建

  1. 普通函数
#include <iostream>
#include <thread>

void func(int a, int& b){
    a = 2;
    b = 2;
}

int main(){
    int a = 1;
    int b = 1;
    //创建子线程
    std::thread newThread(func, a, std::ref(b));
    std::cout << a << “ ” << b << std::endl;
    
    
    //主线程等待子线程结束
    newThread.join();
    
    return 0;
}

编译:
g++ main.cpp -o main -pthread

  1. 成员函数
#include <iostream>
#include <thread>

class MyClass{
public:
    void func(const int& num){
        std::cout << “I am” << num << “ years old” << std::endl;
    }
};

int main(){
    MyClass obj;
    int num = 20;
    std::thread newThread(&MyClass::func, &obj, std::cref(num));
    
    newThread.join();
    
    return 0;
}
  1. 仿函数
#include <iostream>
#include <thread>

class MyClass{
public:
    void operator() (int& num){
        std::cout << “sub-thread start” << std::endl;
        num = 30;
        std::cout << “sub-thread end” << std::endl;
    }
};

int main(){
    std::cout << “main thread start” << std::endl;
    
    MyClass obj;
    int num = 29;
    std::cout << “num= ” << num << std::endl;
    
    std::thread newThread(obj, std::ref(num));
    //也可如下实现
    //std::thread newThread(MyClass(), std::ref(num));
    
    newThread.join();
    
    std::cout << “num= ” << num << std::endl;
    std::cout << “main thread end” << std::endl;
    
    return 0;
}
  1. 匿名函数lambda
#include <iostream>
#include <thread>

int main(){
    auto function = [](int a) {std::cout << a << std::endl;}
    
    std::thread newThread(function, 10);
    //或者以下方式直接调用lambda函数,无需上述语句
    //std::thread newThread([](int a) {std::cout << a << std::endl;}, 10);
    
    newThread.join();
    
    return 0;
}

标签:std,main,cout,thread,int,创建,线程,include,多少
From: https://www.cnblogs.com/forlqy/p/chuang-jian-xian-cheng-you-duo-shao-zhong-fang-shi.html

相关文章

  • 两个线程交替打印一个共享变量
    首先给出基本框架#include<iostream>#include<thread>usingnamespacestd;intmain(){intn=100;inti=0;//创建两个线程threadnewThre......
  • 多线程和多进程的区别
    一个线程从属于一个进程;一个进程可以包含多个线程。一个线程挂掉,对应的进程挂掉,多线程也挂掉;一个进程挂掉,不影响其它进程,多进程稳定。进程系统开销显著大于线程开销;线程......
  • 创建进程的流程及进程切换的情况
    创建进程为新进程分配一个唯一的进程标识符(pid)。为新进程分配资源,如内存空间、文件描述符、信号处理函数等。初始化新进程的进程控制块(PCB),包括设置初始状态、优先级、......
  • 还不知道线程池的好处?快来了解一下
    摘要:线程池的好处:重用存在的线程,减少对象创建、消亡的开销,性能佳;可以有效控制最大并发线程数,提高系统资源利用率,同时可以避免过多资源竞争,避免阻塞。本文分享自华为云社区《......
  • 还不知道线程池的好处?快来了解一下
    摘要:线程池的好处:重用存在的线程,减少对象创建、消亡的开销,性能佳;可以有效控制最大并发线程数,提高系统资源利用率,同时可以避免过多资源竞争,避免阻塞。本文分享自华为云社区......
  • jmeter跨线程组调用变量-以token为例
    跨线程组调用变量的解决方法:在beanshell取样器中使用setProperty函数设置全局变量,其他线程组用P函数调用全局变量 跨线程组调用变量的步骤:以token为例跨线程组调用有两......
  • 第8章:多线程
    第8章多线程1、基本概念:程序、进程、线程程序(program):是为完成特定任务、用某种语言编写的一组指令的集合。即指一段静态的代码,静态对象。进程(process):是程序的一次执......
  • Java——四种线程创建方式
    java中创建线程有四种方式,分别是:继承Thread类,重写run方法,然后创建线程对象并调用start方法。实现Runnable接口,实现run方法,然后创建线程对象并传入Runnable实例,再调用start......
  • 使用WPF创建炫亮按钮
    1.创建三个按钮(创建多个按钮的目的是作对比及样式演示)(1)打开Microsoft ExpressionBlend2(我这里是AugustPreview版本,以下简称Blend)(2)File-> NewProject,默认选......
  • 我嘞个神——原来创建应用根本不需要会编码(看我10分钟应用上线)
    一、前言这里我用到了用友的平台,很多学生们刚毕业都在从事运维和实施的岗位,而且这个平台在我这面也是非常流行的,每年毕业季都会在我们学校这招聘走很多学生,因为很多学生都会......