Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing WaitForMultipleObjects in Qt

Tags:

qt

winapi

porting

I am not familar with WINAPI, and I am looking for a way to replace WaitForMultipleObjects used in one example I'm porting to Qt by anything using Qt only. Is it possible?

EDIT: (Providing more information as requested in comments)

A 3rd party API provides an array of events:

HANDLE  m_hEv[MAX_EV];

In an endles-loop of a thread, the program waits for the events like this:

WaitForMultipleObjects(m_EvMax, m_hEv, FALSE ,INFINITE )

The HANDLE type seems to be void*. So I wonder, if any Qt class could observe m_hEv for changes and unlock thread execution.

like image 658
Valentin H Avatar asked Jun 28 '26 23:06

Valentin H


2 Answers

There is no simple way of porting WaitForMultipleObjects outside WinAPI. WinAPI has an "advantage" of that all lockable resources (sockets, files, processes) provide the same generic non-typesafe HANDLE, which is your void*. Unlike other platforms which have different ways of locking and signalling per the type of resource, the event handling in WinAPI is largely independent of the resources. Then a generic function like WaitForMultipleObjects can exist, which doesn't need to care who produced the HANDLEs. So you'll have to understand what the code is trying to do and mimic it differently per scenario.

The biggest difference is in WaitForMultipleObjects third parameter, which is FALSE in your case. Which means that the it will exit waiting as soon as any single event of the waiting array will happen. That is the easier scenario and can be replaced with a QWaitCondition.

  1. Instead of m_hEv, you will pass a QWaitCondition* into the code which signals the event (most probably via WinAPI SetEvent(m_hEv[x]))
  2. Instead of WaitForMultipleObjects, do QWaitCondition::wait().
  3. Instead of SetEvent(), do QWaitCondition::wakeOne().

Would the third parameter be TRUE, then the WinAPI code waits until ALL m_hEv events are signalled. The established name for such functionality is a synchronization barrier and it can be simulated with QEventCondition too, but does not come out of the Qt box. I never needed to do any myself, but SO has some ideas how to do it:

Qt synchronization barrier?

like image 105
Pavel Zdenek Avatar answered Jul 04 '26 14:07

Pavel Zdenek


WaitForMultipleObjects is a kind of generic function that works with many things: threads, processes, mutexes, etc. Qt is an OOP library where every class exposes the operations it supports. So the equivalent operation in Qt depends on what class you're using. For example, with threads, use QThread::wait. With mutexes, use QMutex::lock.

like image 45
user1610015 Avatar answered Jul 04 '26 16:07

user1610015



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!