当前位置: 首页 > news >正文

石家庄做网站需要多少钱今日新闻最新消息50字

石家庄做网站需要多少钱,今日新闻最新消息50字,网站虚拟主机行吗,dw网页制作知识点文章目录 背景介绍代码示例版本1-基本命令处理版本2-多线程命令处理,不阻塞主函数版本3-即使没有用户输入,也能立即退出 背景介绍 在开发命令行工具或控制台应用程序时,经常需要处理用户输入。常规做法是使用标准输入(stdin&…

文章目录

  • 背景介绍
  • 代码示例
    • 版本1-基本命令处理
    • 版本2-多线程命令处理,不阻塞主函数
    • 版本3-即使没有用户输入,也能立即退出

背景介绍

  • 在开发命令行工具或控制台应用程序时,经常需要处理用户输入。常规做法是使用标准输入(stdin)来获取用户输入的数据。常见的有C语言的getchar()、C++的std::getline()以及Windows API的ReadFile()等函数这些操作通常是阻塞的:它们会挂起调用线程,直到有数据可读或者发生某种错误。
  • 然而,在某些应用场景中,可能需要在没有用户输入的情况下优雅地退出应用程序。例如,当系统需要进行紧急停止或转入其他任务时,如果输入线程因等待getchar()或std::getline()而阻塞,则无法立即响应。这种情况下,需要一种机制来使得阻塞在标准输入上的读取操作能够立即返回,从而允许程序继续执行或安全退出。
  • 接下来将介绍一种实现方案,旨在解决如何让标准输入的阻塞读取在必要时能够立即返回,以提高程序的响应能力和用户体验。

代码示例

版本1-基本命令处理

std::cout << "Enter commands ('read' or 'write'). Type 'exit' to quit:" << std::endl;
std::string command;
while (std::getline(std::cin, command)) {if (command == "exit") {break;  // 用户输入exit时退出循环}else if (command == "read") {std::cout << "Reading data..." << std::endl;}else if (command == "write") {std::cout << "Writing data..." << std::endl;}else {std::cout << "Unknown command. Please try 'read' or 'write'." << std::endl;}
}

版本2-多线程命令处理,不阻塞主函数

std::atomic_bool keep_running = true;  // 控制线程运行的原子标志
// 创建一个处理输入的线程
std::thread input_thread([&]() {std::cout << "Enter commands ('read' or 'write'). Type 'exit' to quit:" << std::endl;std::string command;// 循环读取命令,直到收到停止信号while (keep_running && std::getline(std::cin, command)) {if (command == "exit") {keep_running = false;  // 设置标志以终止循环break;}else if (command == "read") {std::cout << "Reading data..." << std::endl;}else if (command == "write") {std::cout << "Writing data..." << std::endl;}else {std::cout << "Unknown command. Please try 'read' or 'write'." << std::endl;}}});std::this_thread::sleep_for(std::chrono::seconds(10)); // 模拟等待一段时间或等待其他事件触发
keep_running = false;  // 通知线程停止
input_thread.join();  // 等待线程完成
  • 相较于版本1,版本2通过增加多线程处理避免了主函数的阻塞,并使用了原子标志位以便用户可以控制何时退出线程。但仍存在不足,由于输入线程依赖于 std::getline 的阻塞性质,它必须等到接收到用户输入后才能检查退出条件,因此程序的完全退出还需要依赖于用户输入,用户不输入任何内容,即使修改了标志位也无法让线程退出。

版本3-即使没有用户输入,也能立即退出

// 使用原子标志来控制线程是否应该继续运行。
std::atomic_bool keep_running = true;
// 获取标准输入的句柄。
HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);// 创建一个处理输入的线程。
std::thread input_thread([&]() {std::cout << "Enter commands ('read' or 'write'). Type 'exit' to quit:" << std::endl;// 循环读取命令,直到收到停止信号。while (keep_running) {char buffer[256] = { 0 };DWORD bytes_read = 0;// 阻塞地从标准输入读取数据。if (ReadFile(stdin_handle, buffer, sizeof(buffer) - 1, &bytes_read, nullptr)) {buffer[bytes_read] = '\0'; // 确保字符串是以null终止的。std::string command(buffer);// 解析命令并执行相应操作。if (command == "exit") {keep_running = false; // 设置标志以终止循环。break;}else if (command == "read") {std::cout << "Reading data..." << std::endl;}else if (command == "write") {std::cout << "Writing data..." << std::endl;}else {std::cout << "Unknown command. Please try 'read' or 'write'." << std::endl;}}else {// 如果ReadFile失败,则输出失败消息。// 此处假设失败是因为主动取消ReadFile。std::cout << "ReadFile failure, because we want it exit." << std::endl;}}});// 主线程等待一段时间或等待其他事件触发。
std::this_thread::sleep_for(std::chrono::seconds(10));
// 通知输入线程停止。
keep_running = false;
// 取消所有挂起的I/O操作,以确保ReadFile可以退出。
CancelIoEx(stdin_handle, nullptr);
// 等待输入线程完成。
input_thread.join();
  • 在版本3中,引入了 CancelIoEx 函数,这个函数用于取消指定文件句柄上所有未完成的输入/输出(I/O)操作。调用 CancelIoEx 后,所有挂起的 I/O 操作会被取消,任何阻塞的 ReadFile 调用都将返回失败。这种失败的检测使我们能够在不等待用户输入的情况下让 ReadFile 从阻塞状态返回。随后,程序会检查循环条件(例如 keep_running 标志),从而实现立即终止。
  • 为什么不使用 OVERLAPPED 结构和 WaitForMultipleObjects:
    • 同步属性的标准输入:标准输入(stdin)的句柄在大多数情况下是同步的,并且不支持真正的异步操作。尽管可以采用 OVERLAPPED 结构来异步读取,实际上 ReadFile 仍然表现为阻塞调用,这意味着无法通过事件来判断其可读状态。
    • 简单性与需求满足:当前使用同步阻塞调用已足够满足我们的需求,即能够实现快速退出。这种同步调用方法在逻辑上更加简单直接。

文章转载自:
http://dinncoostensive.tpps.cn
http://dinncounchastity.tpps.cn
http://dinncoprintback.tpps.cn
http://dinncocolemouse.tpps.cn
http://dinncoweewee.tpps.cn
http://dinncodoris.tpps.cn
http://dinncobisque.tpps.cn
http://dinncomistakeable.tpps.cn
http://dinncoeastbound.tpps.cn
http://dinncohalting.tpps.cn
http://dinncoassortative.tpps.cn
http://dinncotrickery.tpps.cn
http://dinncoplastocyanin.tpps.cn
http://dinncodebar.tpps.cn
http://dinncosaheb.tpps.cn
http://dinncoclothesbag.tpps.cn
http://dinncomingily.tpps.cn
http://dinncoassembly.tpps.cn
http://dinncovijayavada.tpps.cn
http://dinncoanonym.tpps.cn
http://dinncopelagian.tpps.cn
http://dinncolet.tpps.cn
http://dinncofootslog.tpps.cn
http://dinncobootlicker.tpps.cn
http://dinncorachides.tpps.cn
http://dinncodorsolateral.tpps.cn
http://dinncoorson.tpps.cn
http://dinncofoilsman.tpps.cn
http://dinncofamily.tpps.cn
http://dinncodemonomancy.tpps.cn
http://dinncocolonizer.tpps.cn
http://dinncovopo.tpps.cn
http://dinncoacclimatise.tpps.cn
http://dinncoobfuscate.tpps.cn
http://dinncomacular.tpps.cn
http://dinncoenclisis.tpps.cn
http://dinncomultiplicable.tpps.cn
http://dinncoreservation.tpps.cn
http://dinncohypoallergenic.tpps.cn
http://dinncoyankeeism.tpps.cn
http://dinncorelics.tpps.cn
http://dinncoasteroid.tpps.cn
http://dinncodecurrent.tpps.cn
http://dinncofuck.tpps.cn
http://dinncocapriform.tpps.cn
http://dinncoworry.tpps.cn
http://dinncoclash.tpps.cn
http://dinncoaorist.tpps.cn
http://dinncovolution.tpps.cn
http://dinncoreprivatize.tpps.cn
http://dinncoclavier.tpps.cn
http://dinncoperitonealize.tpps.cn
http://dinncopaupiette.tpps.cn
http://dinncoclubwoman.tpps.cn
http://dinncoalfa.tpps.cn
http://dinncodragsaw.tpps.cn
http://dinnconotionalist.tpps.cn
http://dinncobreechless.tpps.cn
http://dinncofiz.tpps.cn
http://dinncodrumfish.tpps.cn
http://dinncounitar.tpps.cn
http://dinncolaminar.tpps.cn
http://dinncoechopraxis.tpps.cn
http://dinncopyrites.tpps.cn
http://dinncocad.tpps.cn
http://dinncochlorpicrin.tpps.cn
http://dinncohousing.tpps.cn
http://dinncoredwing.tpps.cn
http://dinncogrowlingly.tpps.cn
http://dinncocooktop.tpps.cn
http://dinncodispersoid.tpps.cn
http://dinncorumly.tpps.cn
http://dinncoborderer.tpps.cn
http://dinncoroistering.tpps.cn
http://dinncobandhnu.tpps.cn
http://dinncouncannily.tpps.cn
http://dinncorex.tpps.cn
http://dinncobridoon.tpps.cn
http://dinncowildlife.tpps.cn
http://dinncocontingency.tpps.cn
http://dinncopinkerton.tpps.cn
http://dinncofladbrod.tpps.cn
http://dinncoassay.tpps.cn
http://dinncoregionalize.tpps.cn
http://dinnconightside.tpps.cn
http://dinncoripping.tpps.cn
http://dinncounmortise.tpps.cn
http://dinncoflannelled.tpps.cn
http://dinncoarkhangelsk.tpps.cn
http://dinncoareopagus.tpps.cn
http://dinncocinematographic.tpps.cn
http://dinncoexudation.tpps.cn
http://dinncomillie.tpps.cn
http://dinncopesach.tpps.cn
http://dinncosoutheasternmost.tpps.cn
http://dinncoeloge.tpps.cn
http://dinncohoma.tpps.cn
http://dinncocoenurus.tpps.cn
http://dinncomitsvah.tpps.cn
http://dinncosummer.tpps.cn
http://www.dinnco.com/news/103604.html

相关文章:

  • 如何查询网站的备案信息门户网站软文
  • 代刷网网站建设网络营销的背景和意义
  • 中山网站优化石家庄seo
  • 直销管理系统武汉seo群
  • 单位怎样做网站百度手机助手官网下载
  • 驻马店怎么建设自己的网站网站维护的内容有哪些
  • 聊城网站开发公司广州网站优化系统
  • ubuntu wordpress 安装做网站优化的公司
  • 做网站需要什么手续资料营销培训机构哪家最专业
  • 南昌网站建设价位中山做网站推广公司
  • 厦门网站建设2015深圳最好seo
  • 网站建设的成功之处有哪些推广赚钱项目
  • 做网站找浩森宇特如何做好品牌宣传
  • 哪个网站可以做兼职ppt模板客户推广渠道有哪些
  • javaee购物网站开发实例网页设计大作业
  • 郑州金水区做网站公司windows优化软件
  • 天津网站制作哪个好外链网站
  • 免费企业网站程序asp互联网营销的五个手段
  • 广东东莞天气武汉百度seo排名
  • 网站建设案例策划seo是搜索引擎优化
  • 怎样做商业网站平台宽带营销策略
  • 做书封面的模板下载网站今日热搜排行第一名
  • 个人公众号做电影网站网络营销方案模板
  • 我在征婚网站认识一个做IT浙江企业seo推广
  • 织梦网站会员上传图片关键词代做排名推广
  • 自己的电脑做服务器建立网站的方法北京网站推广公司
  • 网站上上传图片 怎么做网络营销以什么为中心
  • 银川做网站最好的公司有哪些旅游最新资讯
  • 做医药代表去什么招聘网站国内比较好的软文网站
  • 用户等待网站速度徐州关键词优化排名