QWaitCondition与WaitForSingleObject [英] QWaitCondition vs WaitForSingleObject

查看:1277
本文介绍了QWaitCondition与WaitForSingleObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



有没有人知道如何以与WaitForSignleObject相同的方式使用QWaitCondition对象?

我解释得更好:



我经常使用WaitForSingleObject来捕捉上升的事件,而无需等待

使用公式



if(WaitForSingleObject(hEvent,0)== WAIT_OBJECT_0){

//这里有些代码

}



我正在尝试用QWaitCondition做同样的事情,但以下



mutex.lock();

waitevent.wait( &互斥,0);

mutex.unlock();



不起作用。有人可以帮帮我吗?



谢谢,我为我的坏英语道歉

Hello,

does anybody knows how to use QWaitCondition object in the same manner of WaitForSignleObject?
I explain better:

I often used WaitForSingleObject to catch rised events without waiting
using the formula

if(WaitForSingleObject(hEvent,0)==WAIT_OBJECT_0){
//some code here
}

I'm trying to do the same thing with QWaitCondition but the following

mutex.lock();
waitevent.wait(&mutex,0);
mutex.unlock();

doesn't work. someone can help me?

thanks and I apologise for my bad english

推荐答案

其中一个原因使用Qt是跨平台开发。好的Qt项目可以在不同的平台上重建;并且生成的应用程序应该在不同的操作系统上工作。



这个 WaitForSingleObject 是特定于Windows的: http://msdn.microsoft.com/en-us/library /windows/desktop/ms687032%28v=vs.85%29.aspx [ ^ ]。



如果您在代码中使用它,它会破坏你的平台兼容性。



QWaitCondition 是Qt的一部分,旨在抽象出来平台: http://qt-project.org/doc/qt-4.8/qwaitcondition.html [ ^ ]。



现在,我不确定你是否了解线程同步。你正试图在锁内等待。 wait本身就是一个与多个线程一起使用的同步方法。像在锁内等待的组合是创建一些死锁的可靠方法。理论上证明所有线程同步。



-SA
One of the reasons to use Qt is the cross-platform development. Good Qt projects can be rebuild on a different platform; and the resulting application should work on a different OS.

This WaitForSingleObject is Windows-specific: http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032%28v=vs.85%29.aspx[^].

If you use it in your code, it will kill your platform compatibility.

And QWaitCondition is the part of Qt, designed to abstract out the platform: http://qt-project.org/doc/qt-4.8/qwaitcondition.html[^].

Now, I'm not sure you understand thread synchronization. You are trying to wait inside a lock. The wait is itself a synchronization method to be used with multiple threads. The combinations like waiting inside a lock is the sure way to create some deadlocks. All thread synchronization should be theoretically proven.

—SA


这篇关于QWaitCondition与WaitForSingleObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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