热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

有效地等待标志状态更改而不阻塞资源?-Efficientlywaitforaflagstatechangewithoutblockingresources?

Threadtowaitinfinitelyinaloopuntilaflagstatechange,thencallfunction.线程在循环中无限期等待,直到标志状

Thread to wait infinitely in a loop until a flag state change, then call function.

线程在循环中无限期等待,直到标志状态改变,然后调用函数。

pseudo code illustration:

伪代码图:

while (true)
{
    while (!flag)
    {
            sleep(1);
    }
    clean_upfunction();
}

Currently:

  • Using the multithreaded versions of the C run-time libraries only
  • 仅使用C运行时库的多线程版本

No:

  • MFC

Question:

  • Is there a more efficient way of implementing the above
  • 有没有更有效的方法来实现上述

  • A waitForStateChange() - similar to above - in the threading library
  • 一个waitForStateChange() - 类似于上面 - 在线程库中

3 个解决方案

#1


For Windows (which you have this tagged for), you want to look at WaitForSingleObject. Use a Windows Event (with CreateEvent), then wait on it; the other thread should call SetEvent. All native Windows, no MFC or anything else required.

对于Windows(您已将其标记为),您需要查看WaitForSingleObject。使用Windows事件(使用CreateEvent),然后等待它;另一个线程应该调用SetEvent。所有本机Windows,没有MFC或任何其他要求。

#2


If you're not on Windows, and are instead on a POSIXish box, pthread_cond_wait is the best match:

如果您不在Windows上,而是在POSIXish框中,则pthread_cond_wait是最佳匹配:

/* signaler */
    pthread_mutex_lock(mutex);
    flag = true;
    pthread_cond_signal(cond);
    pthread_mutex_unlock(mutex);

/* waiter */
    pthread_mutex_lock(mutex);
    do {
        pthread_cond_wait(cond, mutex);
    } while (!flag);
    pthread_mutex_unlock(mutex);

The classic self-pipe trick is easier and cooler though :) Works on systems without pthreads too.

经典的自管技巧虽然更容易和更酷:)适用于没有pthreads的系统。

/* setup */
    int pipefd[2];
    if (pipe(pipefd) <0) {
        perror("pipe failed");
        exit(-1);
    }

/* signaler */
    char byte = 0;
    write(pipefd[0], &byte, 1);  // omitting error handling for brevity

/* waiter */
    char byte;
    read(pipefd[1], &byte, 1);  // omitting error handling for brevity

The waiter will block on the read (you don't set O_NONBLOCK) until interrupted (which is why you should have error handling) or the signaler writes a byte.

服务员将阻止读取(您不设置O_NONBLOCK)直到被中断(这就是您应该进行错误处理的原因)或者信号器写入一个字节。

#3


Take a look at condition_variable in Boost.Thread.

看看Boost.Thread中的condition_variable。

http://www.boost.org/doc/libs/1_37_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref

It is portable, easier to use than the platform-specific options. Moreover, IIUC, the upcoming C++0x std::condition_variable was modeled after it.

它比便携式设备更容易使用,比特定于平台的选项更容易使用。此外,IIUC,即将推出的C ++ 0x std :: condition_variable是在它之后建模的。


推荐阅读
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 在macOS环境下使用Electron Builder进行应用打包时遇到签名验证失败的问题,具体表现为签名后spctl命令检测到应用程序未通过公证(Notarization)。本文将详细探讨该问题的原因及解决方案。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 在 Windows 10 中,F1 至 F12 键默认设置为快捷功能键。本文将介绍几种有效方法来禁用这些快捷键,并恢复其标准功能键的作用。请注意,部分笔记本电脑的快捷键可能无法完全关闭。 ... [详细]
  • 本文详细介绍了如何使用Python编写爬虫程序,从豆瓣电影Top250页面抓取电影信息。文章涵盖了从基础的网页请求到处理反爬虫机制,再到多页数据抓取的全过程,并提供了完整的代码示例。 ... [详细]
author-avatar
到几百元_309
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有