C++11/std::thread – 线程的基本用法
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++11/std::thread – 线程的基本用法
原文链接:https://www.stubbornhuang.com/777/
发布于:2020年04月01日 11:57:12
修改于:2020年04月01日 11:57:12

1 获取CPU核心数量
使用std::thread::hardware_concurrency()获取当前CPU核心数量。
代码示例:
#include <iostream>
#include <thread>
int main()
{
std::cout << std::thread::hardware_concurrency() << std::endl;
getchar();
return 0;
}
2 获取当前线程ID
get_id()可用于获取当前线程的id。
代码示例:
#include <iostream>
#include <thread>
int main()
{
std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
std::cout << thread.get_id() << std::endl;
thread.join();
getchar();
return 0;
}
3 线程休眠
3.1 sleep_for让当前线程休眠一段时间
#include <iostream>
#include <thread>
int main()
{
std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << thread.get_id() << std::endl;
thread.join();
getchar();
return 0;
}
3.2 sleep_until让当前线程休眠一直到指定时间
#include <iostream>
#include <iomanip>
#include <thread>
#include <chrono>
#include <ctime>
#pragma warning(disable:4996)//加上可去掉unsafe 请使用localtime_s的编译报错
int main()
{
std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
std::time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
std::cout << "当前时间为: " << std::put_time(ptm, "%X") << std::endl;
// 设定线程被唤醒时间为1分钟后
ptm->tm_min += 1;
std::this_thread::sleep_until(std::chrono::system_clock::from_time_t(mktime(ptm)));
std::cout << thread.get_id() << std::endl;
std::cout << "线程重新被唤起时间为:" << std::put_time(ptm, "%X") << std::endl;;
thread.join();
getchar();
return 0;
}
4 yield
yield放弃当前线程执行,回到准备状态,重新分配cpu资源。所以调用该方法后,可能执行其他线程,也可能还是执行该线程。
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
bool ready = false;
std::thread thread{ [&] {
while (!ready)
{
std::cout << "线程函数被挂起" << std::endl;
std::this_thread::yield();
ready = true;
}
std::cout << "线程函数被启动" << std::endl;
} };
thread.join();
getchar();
return 0;
}
当前分类随机文章推荐
- C++ – 字节数组byte[]或者unsigned char[]与short的相互转换 阅读944次,点赞0次
- C++ - const修饰符与指针 阅读189次,点赞1次
- C++11 - 构建一个符合实际应用要求的线程池 阅读961次,点赞0次
- C++ - linux编译C++代码出现error: use of deleted function std::atomic
::atomic(const std::atomic 阅读1947次,点赞0次&) - C++ - 字节数组byte[]或者unsigned char[]与int的相互转换 阅读6019次,点赞1次
- C++11 - 父类与子类相互包含的时候该如何正确的使用智能指针,防止循环引用 阅读2284次,点赞0次
- C++ 回调函数 阅读2763次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与long double的相互转换 阅读806次,点赞0次
- C++ - vector存储动态指针时正确释放内存 阅读5312次,点赞0次
- C++ - 左值和右值,右值引用与移动语义的概念与理解 阅读125次,点赞1次
全站随机文章推荐
- 资源分享 - 3D游戏与计算机图形学中的数学方法 第3版 , Mathematics for 3D Game Programming and Computer Graphics, Third Edition 中文版PDF下载 阅读1011次,点赞0次
- Duilib - 在主界面xml描述文件中使用ChildLayout或者Include嵌入子界面xml描述文件 阅读316次,点赞0次
- 资源分享 - The HDRI Handbook 2.0- High Dynamic Range Imaging for Photographers and CG Artists高清PDF下载 阅读2078次,点赞0次
- 资源分享 - GPU Pro 360 - Guide to Rendering 英文高清PDF下载 阅读1978次,点赞0次
- C++11 - 使用std::thread在类内部以成员函数作为多线程函数执行异步操作 阅读1870次,点赞0次
- 资源分享 - 计算机图形学 - 原理及实践 基础篇 原书第3版 , Computer Graphics Principles and Practice (Third Edition) 中文版PDF下载 阅读2059次,点赞2次
- 资源分享 - Game AI Pro 360 - Guide to Architecture 英文高清PDF下载 阅读1940次,点赞0次
- Python - 使用Opencv-Python库获取本机摄像头视频并保存为视频文件 阅读2362次,点赞0次
- 资源分享 - 深度学习 花书 AI圣经(Deep Learning) 中文PDF下载 阅读4496次,点赞1次
- Python - 使用flask_sockets库构建websocket服务器 阅读2766次,点赞0次
评论
164