IE盒子

搜索
查看: 112|回复: 0

C++ 多线程

[复制链接]

4

主题

10

帖子

20

积分

新手上路

Rank: 1

积分
20
发表于 2023-3-3 21:15:23 | 显示全部楼层 |阅读模式
多线程

多线程编程的头文件

<thread>,<mutex>,<atomic>,<condition_variable>,<future>
<thread>

std::thread的构造

thread类的构造函数:
class thread{
    thread();  //默认构造函数

    thread(std::Function&& f, Args&&... args); //初始化构造函数

    thread(thread&& other);

    //...
}
<hr/>初始化构造函数:
void fun1(){
    cout<<"thread : fun1"<<endl;
}
void fun2(int i){
    cout<<"thread : fun2  " << i * i << endl;
}
int main(){
    std::thread t1(fun1);
    std::thread t2(fun2,9);
    t1.join();
    t2.join();
    return 0;
}


move构造函数:
thread t(fun1);

thread t1(move(t));

assert(t.joinable() == false);
thread& t2 = t1;

t2.join();  // or t1.join();thread不能拷贝赋值,下面两种方式均错误:
thread t(function);

//thread t1 = t;  错误
//thread t1(t);   错误
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表