【c++知识点补充】多线程1

1、创建线程

//把使用函数添加到线程
eg:把函数 vooid first(int a);加入线程myThread

//method one
std::thread myThread(first);
myThread.join();

//method two
//通过一个成员函数创建线程(在构造函数中操作)。
//补充std::bind()用法,第一个参数表示对象的成员函数的指针,第二个参数表示对象的地址,后边为函数所需参数,可用占位符
//补充:占位符,_1,_2,_3....._N依次,代表此处有变量输入输出,这个参数以调用时传入的参数为准[占位符用法](http://t.csdn.cn/R9Zsg)
class Hello {
public:
  Hello() {
    std::thread t(std::bind(&Hello::Entry, this, "World"));//std::thread t(std::bind(&Hello::Entry, this, std::placeholders::_1));
    t.join();
  }

private:
  // 线程函数
  void Entry(const char* what) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Hello, " << what << "!" << std::endl;
  }
};
 
//method three 
std::thread (thread_fun,1).detach();//直接创建线程,没有名字
//函数形式为void thread_fun(int x)

2、join与detach方式

detach方式,启动的线程自主在后台运行,当前的代码继续往下执行,不等待新线程结束。
join方式,等待启动的线程完成,才会继续往下执行。
//join例子
eg:
#include <iostream>
#include <thread>
using namespace std;
void thread_1()
{
    while(1)
    {
        cout<<"子线程1"<<endl;
    }
}
void thread_2(int x)
{
    while(1)
    {
        cout<<"子线程2"<<endl;
    }
}
int main()
{
    thread first ( thread_1);     // 开启线程,调用:thread_1()
    thread second (thread_2,100);  // 开启线程,调用:thread_2(100)

    first.join();                // pauses until first finishes 这个操作完了之后才能destroyed
    second.join();               // pauses until second finishes//join完了之后,才能往下执行。
    while(1)
    {
        std::cout << "主线程\n";
    }
    return 0;
}
//detach例子
//主线程不会等待子线程结束。如果主线程运行结束,程序则结束
#include <iostream>
#include <thread>
using namespace std;
void thread_1()
{
    while(1)
    {
        cout<<"子线程1111"<<endl;
    }
}
void thread_2(int x)
{
    while(1)
    {
        cout<<"子线程2222"<<endl;
    }
}
int main()
{
    thread first ( thread_1);     // 开启线程,调用:thread_1()
    thread second (thread_2,100);  // 开启线程,调用:thread_2(100)

    first.detach();                
    second.detach();            
    for(int i = 0; i < 10; i++)
    {
        std::cout << "主线程\n";
    }
    return 0;
}
但是第两个运行出的结果让我有点疑惑
1、第一个join运行的时候是一会输出"线程1"一会输出线程2;
2、但第二个detach运行的时候是先输出的主线程,按理说不是应该至少先输出几个"线程1""线程2" 再输出主线程吗?
3、是有时候还没有输出换行,比如线程1线程2然后来了一个空白行,这里盲猜是两个线程同时进行,endl还没来及的输出的时候线程2的“线程2”已经输出了导致的,稍后验证一下。

在这里插入图片描述

3、this_thread

有四个功能函数
1、get_id
2、yield
3、sleep_for
4、sleep_until
1、get_id	std::this_thread::get_id()	获取线程id
eg:
void func()
{
    cout << "子线程: " << this_thread::get_id() << endl;
}

int main()
{
    cout << "主线程: " << this_thread::get_id() << endl;
    thread t(func);
    t.join();
}
2、yield	std::this_thread::yield()	放弃线程执行,回到就绪状态

yield

//补充c++多线程中的状态

c++状态

c++状态

void func()
{
    for (int i = 0; i < 100000000000; ++i)
    {
        cout << "子线程: " << this_thread::get_id() << ", i = " << i << endl;
        this_thread::yield();
    }
}
3、sleep_for	 std::this_thread::sleep_for(std::chrono::seconds(n)); 	暂停n秒
eg:
void func()
{
    for (int i = 0; i < 10; ++i)
    {
        this_thread::sleep_for(chrono::seconds(1));
        cout << "子线程: " << this_thread::get_id() << ", i = " << i << endl;
    }
}

4、sleep_until()
sleep_until():指定线程阻塞到某一个指定的时间点 time_point类型,之后解除阻塞
sleep_for():指定线程阻塞一定的时间长度 duration 类型,之后解除阻塞
eg:
void func()
{
    for (int i = 0; i < 10; ++i)
    {
        // 获取当前系统时间点
        auto now = chrono::system_clock::now();
        // 时间间隔为2s
        chrono::seconds sec(2);
        // 当前时间点之后休眠两秒
        this_thread::sleep_until(now + sec);
        cout << "子线程: " << this_thread::get_id() << ", i = " << i << endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值