使用boost和stl分别实现超时功能



使用boost和stl分别实现超时功能


10秒钟内输入一个字符。
 如果没输入字符,则打印超时消息;
 如果输入了字符,则发出通知信号,并显示输入所用的时间。


使用stl实现,需要支持C++11

#include <stdlib.h>
#include <string>
#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
//----------------------------------------------------------
std::mutex g_mutexWait;                 // 互斥锁
std::condition_variable g_condWait;     // 条件变量
//----------------------------------------------------------
///< 输入数据线程函数
void InputThread()
{
    std::cout << "请在10秒内输入任意字符:" << std::endl;
    // 等待手工输入
    std::string strInputData = "";
    std::cin >> strInputData;
    // 输入了字符,则发出通知
    if (strInputData != "")
    {
        g_condWait.notify_one();
    }
}
//----------------------------------------------------------
///< 主函数
int main(int argc, char* argv[])
{
    try
    {
        // 启动线程输入数据
        std::thread threadInput(InputThread);
        // 取得当前时间
        time_t tmInputStart = time(NULL);

        // 使用条件变量,等待输入数据
        std::unique_lock<std::mutex> lockWait(g_mutexWait);
        std::cv_status cvsts = g_condWait.wait_for(lockWait, std::chrono::seconds(10));
        // 消息接收超时
        if (cvsts == std::cv_status::timeout)
        {
            std::cout << "您输入的太慢了!请输入任意字符退出程序!" << std::endl;
        }
        else // 接收到条件变量信号,未超时
        {
            time_t tmInputEnd = time(NULL);
            std::cout << "您输入的太快了!只用了" << (tmInputEnd - tmInputStart) << "秒!" << std::endl;
        }
        // 等待线程退出
        threadInput.join();
    }
    catch (std::exception &ex)
    {
        std::cout << ex.what() << std::endl;
    }
    system("PAUSE");
    return 0;
}
//----------------------------------------------------------


使用boost实现

#include "boost/thread.hpp"
#include "boost/thread/mutex.hpp"
#include "boost/thread/condition.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
//----------------------------------------------------------
boost::mutex g_mutexWait;               // 互斥锁
boost::condition_variable g_condWait;   // 条件变量
//----------------------------------------------------------
///< 输入数据线程函数
void InputThread()
{
    std::cout << "请在10秒内输入任意字符:" << std::endl;
    // 等待手工输入
    std::string strInputData = "";
    std::cin >> strInputData;
    // 输入了字符,则发出通知
    if (strInputData != "")
    {
        g_condWait.notify_one();
    }
}
//----------------------------------------------------------
///< 主函数
int main(int argc, char* argv[])
{
    try
    {
        // 启动线程输入数据
        boost::thread threadInput(InputThread);
        // 取得当前时间
        time_t tmInputStart = time(NULL);

        // 使用条件变量,等待输入数据
        //boost::unique_lock<boost::mutex> lockWait(g_mutexWait);
        boost::mutex::scoped_lock lockWait(g_mutexWait);
        bool bRet = g_condWait.timed_wait(lockWait, boost::get_system_time() + boost::posix_time::seconds(10));
        // 消息接收超时
        if (bRet == false)
        {
            std::cout << "您输入的太慢了!请输入任意字符退出程序!" << std::endl;
        }
        else // 接收到条件变量信号,未超时
        {
            time_t tmInputEnd = time(NULL);
            std::cout << "您输入的太快了!只用了" << (tmInputEnd - tmInputStart) << "秒!" << std::endl;
        }
        // 等待线程退出
        threadInput.join();
    }
    catch (std::exception &ex)
    {
        std::cout << ex.what() << std::endl;
    }
    system("PAUSE");
    return 0;
}
//----------------------------------------------------------


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值