在循环中执行/停留一定的时间,例如1分钟 我想制作一个C++程序,运行,同时循环,定义一段时间,然后退出循环。 让循环在运行时做一些事情,当达到设定的时间时,它将打破循环。例如,当循环运行时,它将继续在屏幕上打印内容,当1分钟过去时,它将退出循环 #include <iostream> #include <time.h> using namespace std; int main() { time_t now = time(nullptr); tm* current_time = localtime(&now); int current_minute = current_time->tm_min; int defined_minute = current_minute + 1; while (current_minute < defined_minute) { current_minute = current_time->tm_min; } } #包括 #包括 使用名称空间std; int main() { time\u t now=时间(nullptr); tm*当前时间=本地时间(&now); int current\u minute=当前时间->tm\u min; 整数定义的分钟数=当前分钟数+1; while(当前分钟tm分钟; } }

在循环中执行/停留一定的时间,例如1分钟 我想制作一个C++程序,运行,同时循环,定义一段时间,然后退出循环。 让循环在运行时做一些事情,当达到设定的时间时,它将打破循环。例如,当循环运行时,它将继续在屏幕上打印内容,当1分钟过去时,它将退出循环 #include <iostream> #include <time.h> using namespace std; int main() { time_t now = time(nullptr); tm* current_time = localtime(&now); int current_minute = current_time->tm_min; int defined_minute = current_minute + 1; while (current_minute < defined_minute) { current_minute = current_time->tm_min; } } #包括 #包括 使用名称空间std; int main() { time\u t now=时间(nullptr); tm*当前时间=本地时间(&now); int current\u minute=当前时间->tm\u min; 整数定义的分钟数=当前分钟数+1; while(当前分钟tm分钟; } },c++,C++,我创建了这段代码,它应该可以工作,但它没有,我尝试调试,但我仍然不明白为什么它不工作 另外,这是我第一次在网上发布编程问题,如果有人告诉我如何更好地解决我的问题,我将不胜感激。使用\include auto t0=std::chrono::system_clock::now(); while((std::chrono::system_clock::now()-t0) #include <iostream> #include <future> //std::asyn

我创建了这段代码,它应该可以工作,但它没有,我尝试调试,但我仍然不明白为什么它不工作

另外,这是我第一次在网上发布编程问题,如果有人告诉我如何更好地解决我的问题,我将不胜感激。

使用
\include

auto t0=std::chrono::system_clock::now();
while((std::chrono::system_clock::now()-t0)这个代码片段至少需要C++ 11,它可能会有点复杂的C++对象,但这里是我如何想象它的< /P>
#include <iostream>
#include <future>     //std::async
#include <atomic>
#include <chrono>
#include <sstream>

int main()
{   
    int i = 0;
    std::stringstream sstr;
    std::atomic<bool> do_it(true);

    //store future so that async returns immediately
    //see Notes paragraph on https://en.cppreference.com/w/cpp/thread/async
    auto fut = std::async(std::launch::async, [&do_it]() {
                                                        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                                                        do_it = false;
                                                    });

    //Preemtion could steal some time here...

    while ( do_it )
    {
        sstr << "Iteration: " << ++i << "\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    std::cout << sstr.str() << "\n" << std::flush;

    std::cout << "Bye\n" << std::flush;
}

希望能有所帮助这里是最简单的答案:

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    auto finish = system_clock::now() + 1min;
    do
    {
        cout << "stuff\n";
    } while (system_clock::now() < finish);
}
#包括
#包括
int
main()
{
使用名称空间std;
使用名称空间std::chrono;
自动完成=系统时钟::现在()+1分钟;
做
{

cout另一个答案可能是使用条件变量来处理(…)的难看睡眠,就像在我的第一个答案中经常使用的那样,由于应用程序/计算所花费的时间(这里在stringstream中打印一些循环编号),导致迭代次数只有98次而不是100次

条件变量可以针对多个线程

#include <iostream>
#include <thread>
#include <condition_variable>
#include <atomic>
#include <chrono>
#include <sstream>

//From https://en.cppreference.com/w/cpp/thread/condition_variable/wait_until

int index[2] = { 0 };
std::stringstream sstr;
std::condition_variable cv;
std::mutex cv_m;
std::atomic<bool> do_it(true);

std::mutex mut_print;

void foo(char id, int * index)
{ 
    if ( ! index ) return;

    auto new_tp = std::chrono::system_clock::now();

    while ( do_it )
    {   
        {
            std::lock_guard<std::mutex> lock(mut_print);
            sstr << "Iteration of "<< id << ": " << ++(*index) << "\n";
        }

        new_tp += std::chrono::milliseconds(10);

        while ( do_it )
        {   
            std::unique_lock<std::mutex> lk(cv_m);
            if(std::cv_status::timeout == cv.wait_until(lk,new_tp ) ) {
                break;
            }
        }
    }
}

void signals()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    do_it = false;
    cv.notify_all();
}

int main()
{   
    std::thread t1(foo,'A',&index[0]),t2(foo,'B',&index[1]),ts(signals);

    t1.join();
    t2.join();
    ts.join();

    std::cout << sstr.str() << "\n" << std::flush;

    std::cout << "Bye\n" << std::flush;
}

这一次我们进行了101次迭代,但这个例子并没有假装执行有限的/给定的次数

你说的“它不工作”是什么意思?你的循环没有意义。涉及的变量都没有改变它们在循环中的值,所以循环条件是一个常量(始终为真)。您需要在while循环中立即更新
当前时间。如果您输入了一些日志,您将看到它从未更改。通过“它不工作”我的意思是循环将是无限的。我运行了代码,但它会立即退出循环。我不知道为什么,我从未使用过库,我会深入一点来尝试理解它。ThanksIt可能不起作用,因为在大多数系统上,它只会等待至少60纳秒。我会放弃
计数()
并将差异与
std::chrono::minutes{1}进行比较
。谢谢Howard,非常感谢你的帮助。我偷看了一下你给我的教程链接,这是一个1小时的视频,我稍后会看,我对这个库很兴奋。我运行了代码,它工作得很好,正是我想要的。非常感谢你Hello Howard,我在笔记本电脑前度过了整个过程(昨天也是如此)试图找到从dropbox(或任何其他云存储)下载文件的方法使用C++没有运气。我在这里发布了一个问题,但是没有人回答我,完全解决了我的问题。我就是这样找到联系你的。希望它没有被挪用,如果我很抱歉。如果你能帮助我,我会很感激。谢谢你,我不是这个领域的专家,但是我在这个链接上有代码,使用<代码> CURL 要从网站下载文件:
curl
的替代方案包括
boost::asio
boost::beast
(这是
boost::asio
之上的一层)从我可以理解的是,你使用的是Boost库,我认为自己仍然是C++初学者,我不喜欢开始使用LIS库,我想建立一个更强大的骨干/基础,然后我真的很感谢你花时间回答我这个HowardAs我从来没有使用过的库,这样的代码有点压倒我会对这些图书馆做一些研究,并理解它们,谢谢
#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    auto finish = system_clock::now() + 1min;
    do
    {
        cout << "stuff\n";
    } while (system_clock::now() < finish);
}
#include <iostream>
#include <thread>
#include <condition_variable>
#include <atomic>
#include <chrono>
#include <sstream>

//From https://en.cppreference.com/w/cpp/thread/condition_variable/wait_until

int index[2] = { 0 };
std::stringstream sstr;
std::condition_variable cv;
std::mutex cv_m;
std::atomic<bool> do_it(true);

std::mutex mut_print;

void foo(char id, int * index)
{ 
    if ( ! index ) return;

    auto new_tp = std::chrono::system_clock::now();

    while ( do_it )
    {   
        {
            std::lock_guard<std::mutex> lock(mut_print);
            sstr << "Iteration of "<< id << ": " << ++(*index) << "\n";
        }

        new_tp += std::chrono::milliseconds(10);

        while ( do_it )
        {   
            std::unique_lock<std::mutex> lk(cv_m);
            if(std::cv_status::timeout == cv.wait_until(lk,new_tp ) ) {
                break;
            }
        }
    }
}

void signals()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    do_it = false;
    cv.notify_all();
}

int main()
{   
    std::thread t1(foo,'A',&index[0]),t2(foo,'B',&index[1]),ts(signals);

    t1.join();
    t2.join();
    ts.join();

    std::cout << sstr.str() << "\n" << std::flush;

    std::cout << "Bye\n" << std::flush;
}
Iteration of A: 1
Iteration of B: 1
Iteration of A: 2
Iteration of B: 2
...
Iteration of A: 100
Iteration of B: 100
Iteration of A: 101
Iteration of B: 101

Bye