c ++ cout命令被冻结

时间:2017-03-20 19:55:40

标签: c++ visual-studio-2012

我想在特定时刻打印出当前时间,我每隔5秒检查当前分钟然后进入睡眠状态。听起来很简单,但问题是:2小时后," cout" line被冻结了,这是代码:

#include "stdafx.h"
#include <ctime>
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    struct tm timenow;
    time_t t;
    do 
    {
        t = time(0);
        localtime_s(&timenow,&t);
        if (timenow.tm_min == 30)
        {
            cout << "current hour = " << timenow.tm_hour << endl;//frozen here
        }
        Sleep(5000);
    } while (!(timenow.tm_hour == 20 && timenow.tm_min == 00 ));
    return 0;
}

然后我调试代码,看起来像在write.c中停止,在以下行:

            /* write the lf buf and update total */
            if ( WriteFile( (HANDLE)_osfhnd(fh),
                        lfbuf,
                        (int)(q - lfbuf),
                        (LPDWORD)&written,
                        NULL) )

任何人都可以帮助我吗?提前谢谢!

0 个答案:

没有答案