c++

简介: this 全局函数

get_id()
'get_id()'函数用于获取线程id

include // std::cout

include // std::thread, std::thread::id, std::this_thread::get_id

include // std::chrono::seconds

std::thread::id main_thread_id = std::this_thread::get_id();

void is_main_thread() {
if ( main_thread_id == std::this_thread::get_id() )
std::cout << "This is the main thread.\n";
else
std::cout << "This is not the main thread.\n";
}

int main()
{
is_main_thread();
std::thread th (is_main_thread);
th.join();
}
yield()
该函数的用途是让出该线程已获取的时间片,交给其他线程使用。

include // std::cout

include // std::thread, std::this_thread::yield

include // std::atomic

std::atomic ready (false);

void count1m(int id) {
while (!ready) { // wait until main() sets ready...
std::this_thread::yield();
}
for (volatile int i=0; i<1000000; ++i) {}
std::cout << id;
}

int main ()
{
std::thread threads[10];
std::cout << "race of 10 threads that count to 1 million:\n";
for (int i=0; i<10; ++i) threads[i]=std::thread(count1m,i);
ready = true; // go!
for (auto& th : threads) th.join();
std::cout << '\n';

return 0;
}
sleep_until()
调用线程应恢复其执行的时间点。 请注意,多线程管理操作可能会导致超出此范围的某些延迟。 是表示特定绝对时间的对象

include // std::cout

include // std::put_time

include // std::this_thread::sleep_until

include // std::chrono::system_clock

include // std::time_t, std::tm, std::localtime, std::mktime

int main()
{
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());

struct std::tm * ptm = std::localtime(&tt);
std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n';

std::cout << "Waiting for the next minute to begin...\n";
++ptm->tm_min; ptm->tm_sec=0;
std::this_thread::sleep_until (system_clock::from_time_t (mktime(ptm)));

std::cout << std::put_time(ptm,"%X") << " reached!\n";

return 0;
}
代码的作用是获取当前时间,并等待到下一分钟开始时输出该时间。

代码解释:

include 部分包含了所需的头文件,用于输入输出、时间操作和线程操作。

main() 函数是程序的入口点。
using std::chrono::system_clock; 声明使用命名空间 std::chrono::system_clock,用于处理时间相关的操作。
std::time_t tt = system_clock::to_time_t(system_clock::now()); 获取当前时间并将其转换为 std::time_t 类型的变量 tt。
struct std::tm * ptm = std::localtime(&tt); 使用 std::localtime() 函数将 std::time_t 类型的时间转换为本地时间,并将结果存储在指向 std::tm 结构体的指针 ptm 中。
std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n'; 输出当前时间,使用 std::put_time() 函数将 std::tm 结构体格式化为字符串。
++ptm->tm_min; ptm->tm_sec=0; 将下一分钟的时间设置为等待目标,即将 ptm 结构体中的分钟加一,并将秒数设置为零。
std::this_thread::sleep_until(system_clock::from_time_t(mktime(ptm))); 使用 std::this_thread::sleep_until() 函数使线程休眠直到指定的时间点,这里使用 system_clock::from_time_t() 函数将 ptm 结构体转换为 std::chrono::system_clock::time_point 类型的时间点。
std::cout << std::put_time(ptm,"%X") << " reached!\n"; 输出达到目标时间点时的时间。
总结:该段代码获取当前时间并等待到下一分钟开始时输出该时间。

sleep_for()
// this_thread::sleep_for示例

include // 标准输入输出流库,用于输出信息

include // 线程库,用于线程操作

include // 时间库,用于时间操作

int main()
{
std::cout << "倒计时开始:\n";
for (int i=10; i>0; --i) {
std::cout << i << std::endl; // 输出当前倒计时的数字
std::this_thread::sleep_for (std::chrono::seconds(1)); // 线程休眠1秒钟
}
std::cout << "发射!\n";

return 0;
}
这段代码演示了如何使用C++标准库中的this_thread::sleep_for函数进行线程休眠操作。代码的功能是进行一个简单的倒计时,从10开始递减到1,每次输出当前的数字,并在输出后暂停1秒钟,然后再继续下一个数字的输出。最后输出"发射!"表示倒计时结束。

在循环中,通过std::this_thread::sleep_for(std::chrono::seconds(1))来使当前线程休眠1秒钟。std::this_thread::sleep_for接受一个std::chrono::duration类型的参数,用于指定休眠的时间长度。在这里,我们使用std::chrono::seconds(1)来表示1秒钟的时间间隔。

通过使用线程休眠,我们可以在倒计时过程中添加一定的延迟,以控制输出的速度和时间间隔。这对于模拟实际场景或者需要等待一段时间后执行某些操作的情况非常有用

相关文章
|
5月前
|
缓存 算法 开发者
【Conan 入门教程 】了解 Conan2.1 中内置部署策略
【Conan 入门教程 】了解 Conan2.1 中内置部署策略
110 1
|
5月前
|
存储 算法
算法思想总结:模拟算法
算法思想总结:模拟算法
|
C语言 C++
C/C++格式化输入与输出(位宽,左右对齐,%d,%f等)总结
C/C++格式化输入与输出(位宽,左右对齐,%d,%f等)总结
1229 0
|
前端开发 JavaScript 大数据
浅谈列表虚拟化技术
列表虚拟化是一项旨在提升 Web 应用性能的关键技术。在当今数字化时代,大量数据的展示已成为 Web 应用开发的常见需求。然而,传统的渲染方式对于大数据集的处理效率并不高,容易导致页面卡顿和性能问题。为了应对这一挑战,列表虚拟化技术应运而生。通过智能地渲染和加载可见区域的元素,列表虚拟化极大地提升了页面渲染速度和用户交互体验。本篇博客将深入探讨列表虚拟化的优点和缺点、适用场景、开源实现方案以及在知名项目中的应用,为有经验的 Web 软件工程师们提供宝贵的指导和启发。让我们一同进入列表虚拟化的世界,为我们的 Web 应用带来更卓越的性能和用户体验
245 0
手把手教你怎么写顺序表 1
手把手教你怎么写顺序表
【C++11】C++多线程之条件变量,异步启动任务(1)
【C++11】C++多线程之条件变量,异步启动任务(1)
131 0
|
存储 缓存 Java
NDK | 带你点亮 JNI 开发基石符文 (一)
NDK | 带你点亮 JNI 开发基石符文 (一)
134 0
NDK | 带你点亮 JNI 开发基石符文 (一)
|
开发工具 git
Git回滚代码到某个commit
回退命令:$ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到前3次提交之前,以此类推,回退到n次提交之前 $ git reset --hard commit_id 退到/进到 指定commi...
9361 0
|
JSON 数据格式
Nuxt.js打印对象不完整[Object]
Nuxt.js打印对象不完整[Object]
56 0