std::this_thread::sleep_for (c++11)

c++ 標準で sleep が加わりました。これでWindowsのsleepを使用せず、標準だけで組めます。

 

 

 

1. sleep_for を使ってみる

sleep_for を使ってみます。

 

評価環境:

参考URL: http://ja.cppreference.com/w/cpp/thread/sleep_for

 

#include "stdafx.h"

#include <iostream>
#include <string>
#include <chrono>               // chrono::seconds, chrono::milliseconds, chrono::microseconds
#include <thread>               // sleep_for

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello waiter." << endl;        
                
    {
        std::chrono::milliseconds       dura(2000);
        std::this_thread::sleep_for( dura );
        cout << "2000ms waited." << endl;
    }
    {
        std::chrono::seconds            dura(2);
        std::this_thread::sleep_for(dura);
        cout << "2s waited." << endl;
    }
    {
        std::chrono::microseconds       dura(2000000);
        this_thread::sleep_for(dura);
        cout << "2000000μs waited." << endl;
    }

    return EXIT_SUCCESS;
}

 

実行結果:

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

MIT License

Copyright (c) 2012-2022 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 

 

参考

 



変更履歴

2022-09-13 - ページデザイン更新
2012-12-09 - 新規作成

 

Programming Items トップページ

プライバシーポリシー