安全使用volatile BOOL强制另一个线程等待? (C ++) [英] Safe to use volatile bool to force another thread to wait? (C++)

查看:884
本文介绍了安全使用volatile BOOL强制另一个线程等待? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过有关挥发性一切说,这是从来没有安全的,但我还是觉得倾向于尝试它,我还没有看到这种特定情况下宣布不安全的。

Everything I've read about volatile says it's never safe, but I still feel inclined to try it, and I haven't seen this specific scenario declared unsafe.

我有一个单独的线程呈现的场景,从主线程模拟提取数据。这有没有同步,并能正常工作。

I have a separate thread that renders a scene, pulling data from the main simulation thread. This has no synchronization, and works fine.

问题是,当程序退出,然后渲染需要停止从模拟线程获取数据之前模拟线程可以安全地清理自己了,而不会引起渲染器试图读取无效的内存。

The issue is that when the program exits, then renderer needs to stop pulling data from the simulation thread before the simulation thread can safely clean itself up without causing the renderer to attempt reading invalid memory.

要做到这一点,我在它的线程运行无限渲染:

To accomplish this, I have the renderer run infinitely in its thread:

volatile bool stillRendering;

void RenderThreadFunction()
{
    stillRendering = true;

    while(programRunning)
    {
        renderer->render();
    }

    stillRendering = false;
}

在主程序线程,收到windproc退出消息的时候,我做的:

In the main program thread, when the windproc quit message is received, I do:

void OnQuit()
{
    programRunning = false;
    while(stillRendering)
    {
    }

    delete application;
}

这样做的目的是确保渲染器停止呼吁删除应用程序之前从应用程序中提取数据。

The goal of this is to be sure the renderer stops pulling data from the application before calling delete on the application.

我第一次尝试这样做没有任何挥发性的关键字,并在调试模式下工作,但是在释放模式它挂。我认为做了一些优化,导致程序停止检查stillRendering价值的编译器。

I first tried this without any volatile keywords, and it worked in debug mode, but in release mode it hung. I assume the compiler made some optimization that causes the program to stop checking the value of stillRendering.

添加挥发性只是stillRendering造成应用程序成功退出每次到目前为止,我已经进行了测试。我不能确定为什么它似乎并不重要,如果programRunning是易失。

Adding volatile to just stillRendering caused the application to successfully exit everytime I've tested it so far. I'm not certain why it doesn't seem to matter if "programRunning" is volatile.

最后,我不清楚如何方案的性能将通过使用挥发性为stillRendering受到影响。没关系,我如果做stillRendering波动影响OnQuit()的性能,但如果它影响RenderThreadFunction的性能这非常重要,我()

Lastly, I am uncertain how the performance of the program will be impacted by using volatile for "stillRendering". It doesn't matter to me if making stillRendering volatile affects the performance of OnQuit(), but it does matter to me if it affects the performance of RenderThreadFunction()

推荐答案

这是完全不安全的,虽然它可能会与一些工作
编译器。基本上,挥发性只影响它的变量
附着,所以 RendererThreadFunction ,例如,可以设置
stillRendering 假的的说完
renderer->渲染(); 。 (这是真实的,即使这两个
stillRendering programRunning 均不稳定。)的
一个问题probablility非常小,所以测试可能
不会透露它。最后,VC ++的一些版本的的给予
挥发性的原子访问下C ++ 11的语义,在
这种情况下,您的code会工作。 (除非你编译
不同版本的VC ++的,当然。)

It's completely unsafe, although it might work with some compilers. Basically, volatile only affects the variable it's attached to, so RendererThreadFunction, for example, could set stillRendering false before having finished renderer->render();. (This is true even if both stillRendering and programRunning were both volatile.) The probablility of a problem is very small, so testing probably won't reveal it. And finally, some versions of VC++ do give volatile the semantics of an atomic access under C++11, in which case, your code will work. (Until you compile with a different version of VC++, of course.)

由于 renderer->渲染()几乎肯定需要
的时间不可忽略的量,但绝对没有理由
这里不使用条件变量。关于唯一一次
你会使用挥发性对于这样的事情是,如果关机
机构是由一个信号(触发在这种情况下,类型
sig_atomic_t ,而不是布尔,虽然在实践中,
它可能没有任何区别)。在这种情况下,有
不会有两个线程,但是只是渲染线程和
信号处理程序。

Given that renderer->render() almost certainly takes a non-negligible amount of time, there's absolutely no reason for not using a conditional variable here. About the only time you'd use volatile for this sort of thing is if the shutdown mechanism were triggered by a signal (in which case, the type would be sig_atomic_t, and not bool, although in practice, it probably doesn't make any difference). In that case, there wouldn't be two threads, but just the renderer thread and a signal handler.

这篇关于安全使用volatile BOOL强制另一个线程等待? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆