Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Posted

1 solution

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
 
Share this answer
 
Comments
Optimistic76 15-Apr-14 13:26pm    
well,

the fact that i would like to use QWaitCondition instead WaitForsingleObject is just for the reason I want to develop crossed-platform apps.
I don't think I'm making a mistake using a Qwaitcondition object inside a lock. Look better at the link you suggest me to look in the "Detailed Description" section.....
Sergey Alexandrovich Kryukov 15-Apr-14 13:46pm    
This is what I just told you.

No, I did not find a sample of wait inside lock. Don't you mixed it up with "wake"? How about looking yourself?

And I don't care what is written in someone's code samples. I am just warning you that you can easily create a deadlock with such combinations, and, very likely, already created it. Further discussion is only possible in wider context, when you explain your goals and your idea of synchronization schema.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900