83 C++ this_thread的全局函数

C++11引入了this_thread命名空间,提供get_id()获取线程ID,sleep_for()和sleep_until()进行线程休眠,以及yield()让出CPU时间片。示例代码展示了如何使用这些函数,包括等待下一个分钟开始和在子线程中延迟执行。此外,还提到thread类的其他功能如交换线程对象和获取硬件线程上下文的近似数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++11提供了命名空间this_thread来表示当前线程,该命名空间中有四个函数:get_id()、sleep_for()、sleep_until()、yield()。

1)get_id()

thread::id get_id() noexcept;

该函数用于获取线程ID,thread类也有同名的成员函数。

2)sleep_for() VS Sleep(1000) Linux sleep(1)

template

void sleep_for (const chrono::duration& rel_time);

该函数让线程休眠一段时间。

3)sleep_until() 2022-01-01 12:30:35

template

void sleep_until (const chrono::time_point& abs_time);

该函数让线程休眠至指定时间点。(可实现定时任务)

#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
#include <thread>
using namespace std;

void test()
{
    time_t tt = chrono::system_clock::to_time_t(chrono::system_clock::now());
    tm *ptm = localtime(&tt);
    cout << "current time:" << put_time(ptm, "%Y-%m-%d %H:%M:%S") << endl;
    cout << "waiting for the next minute to begin..." << endl;
    ptm->tm_min++;
    ptm->tm_sec = 0;
    this_thread::sleep_until(chrono::system_clock::from_time_t(mktime(ptm)));
    cout << put_time(ptm, "%Y-%m-%d %H:%M:%S") << "reached!" << endl;
}
int main()
{
    test();
    return 0;
}
/* current time:2022-12-02 11:12:24
waiting for the next minute to begin...
2022-12-02 11:13:00reached! */
#include <iostream>
#include <thread>
#include <chrono>
#include <unistd.h>
using namespace std;
//普通函数

void func(int no, const string &str)
{
    cout << "子线程:" << this_thread::get_id() << endl;
    for (int i = 0; i < 10; i++)
    {

        cout << no << " " << str << endl;
        // sleep(1);
        // this_thread::sleep_for(chrono::seconds(1));
        //休眠到当前时间往后1秒
        std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::seconds(1));
    }
}
int main()
{
    thread t1(func, 1111, "hello");
    thread t2(func, 2222, "world");
    cout << "主线程:" << this_thread::get_id() << endl;
    t1.join(); //回收线程t1的资源
    t2.join(); //回收线程t2的资源
}

4)yield()

void yield() noexcept;

该函数让线程主动让出自己已经抢到的CPU时间片。

5)thread类其它的成员函数

void swap(std::thread& other); // 交换两个线程对象。

static unsigned hardware_concurrency() noexcept; // 返回硬件线程上下文的数量。

The interpretation of this value is system- andimplementation- specific, and may not be exact, but just an approximation.

Note that this does not need to match the actualnumber of processors or cores available in the system: A system can supportmultiple threads per processing unit, or restrict the access to its resourcesto the program.

If this value is not computable or well defined,the function returns 0.

线程对象不能拷贝,不能赋值,但是能交换,可以转移。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值