我怎么能让线程睡觉或等待5或任何秒

时间:2017-12-03 00:37:24

标签: c++

我有一个程序,用户给出一个数字将产生多少数字,但程序使得快速的东西像每秒20或40相同的数字使用电脑的时间程序

这是我的代码

#include <iostream>;
#include <ctime>;

int i;
int z;

int main() {
    std::cout << "pls enter a number how much numbers will be generatet" << 
std::endl;
    std::cin >> z;

    while (true){
        srand(time(0)); 
        rand();
        rand();
        rand();
        std::cout << rand() << std::endl;
        i++;
        if (i == z) {
            break;
        }
    //system must sleep
    }

    system("pause");

    return 0;
}

1 个答案:

答案 0 :(得分:1)

在C ++ 11中,您可以使用标准库工具执行此操作: 进口:

#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));

这是在线程中休眠并使用多线程的最基本方法。