使用boost和stl分别实现超时功能
2016-07-25 17:35
411 查看
使用boost和stl分别实现超时功能
10秒钟内输入一个字符。
如果没输入字符,则打印超时消息;
如果输入了字符,则发出通知信号,并显示输入所用的时间。
使用stl实现,需要支持C++11
使用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;
}
//----------------------------------------------------------
使用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;
}
//----------------------------------------------------------
相关文章推荐
- C++ 11 并发: 正确 的使用条件变量-condition_variable
- boost之并发编程
- c++11的mutex unique_lock和lock_guard区别
- c++11多线程编程之condition_variable
- condition variable 和 srwlock的一个例子
- std::thread库的condition_variable进行线程唤醒
- [leetcode] 376. Wiggle Subsequence
- iOSUITableViewCell滑动显示多个按钮
- Vue组件开发分享
- 跟我学Android UI高级之十五碎片Fragment
- 跟我学Android UI高级之十五碎片Fragment
- 跟我学Android UI高级之十五碎片Fragment
- c# 根据枚举Value 获得名称
- Wiggle Subsequence
- 使用EasyUI,关于日期格式的文本框按照正常方式获取不到值的问题
- textAligment、contentVerticalAlignment、contentHorizontalAlignment 和 contentMode 的对比
- 精确获取时间(QueryPerformanceCounter)
- 如何在MenuItem中添加icon图标
- 【maven 报错】maven项目执行maven install时报错Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
- poj 2299 Ultra-QuickSort (树状数组求逆序数)