mercredi 30 janvier 2019

What C++'s equivalent to winapi's MsgWaitForMultipleObjectsEx

As I'm making the transition from native winapi calls to manage thread's message queue to c++ code, I have encountered a question which i can't fully answer.

Given the following code snippet

LRESULT QueueConsumeThread()
        {
            MSG msg = { 0 };

            HANDLE hHandles[] = { hHandle1, hHandle2 };
            while (true)
            {
                DWORD dwRes;
                switch (dwRes = ::MsgWaitForMultipleObjects(_countof(hHandles), hHandles, FALSE, INFINITE, QS_ALLEVENTS))
                {
                case WAIT_OBJECT_0 :
                    DoSomething();
                    break;
                case WAIT_OBJECT_0 + 1:
                    DoSomething2();
                    break;
                case WAIT_OBJECT_0 + _countof(hHandles):
                    ATLASSERT(msg.message == WM_QUIT);
                    return 1;
                }
            }

            return 1;
        }

I have read in many sources that a particular thread should be a associated with a single condition_variable, also, using multiple condition_variables or invoking wait_for() wait_until() doesn't sound too efficient.

The following source suggested implementing a safe_queue using condition_variables, I guess that PeekMessage/GetMessage/MsgWaitForMultipleObject work similarity, but what kind of data each cell of the queue should hold and be to receive event signal?

Edit: Asking this as I have to write a cross-platform application.

Aucun commentaire:

Enregistrer un commentaire