wxMessageQueue::Post never blocks? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

wxMessageQueue::Post never blocks?

Post by Ronald »

According to the code

Code: Select all

wxMessageQueueError Post(const Message& msg)
{
    wxMutexLocker locker(m_mutex);

    wxCHECK( locker.IsOk(), wxMSGQUEUE_MISC_ERROR );

    m_messages.push(msg);

    m_conditionNotEmpty.Signal();

    return wxMSGQUEUE_NO_ERROR;
}
When multiple thread Post simultaniously, some may fail instead of blocking while the queue is busy, is it right?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMessageQueue::Post never blocks?

Post by doublemax »

When multiple thread Post simultaniously, some may fail instead of blocking while the queue is busy, is it right?
No, that would make the class pretty useless. What makes you think so?
Use the source, Luke!
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxMessageQueue::Post never blocks?

Post by Ronald »

doublemax wrote:No, that would make the class pretty useless. What makes you think so?

Code: Select all

wxMutex m;
wxMutexLocker locker1(m);
bool ok1 = locker1.IsOk();
wxMutexLocker locker2(m);
bool ok2 = locker2.IsOk();
I have tried the code, it doesn't block, ok1 == true, ok2 == false.
And no any other function in Post blocks.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMessageQueue::Post never blocks?

Post by doublemax »

That's not a valid test as both locks are done in the same thread.

From the documentation for wxMutex::Lock
Note that if this mutex is already locked by the caller thread, this function doesn't block but rather immediately returns.
Use the source, Luke!
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxMessageQueue::Post never blocks?

Post by Ronald »

doublemax wrote:That's not a valid test as both locks are done in the same thread.
Right, blocks in difference threads. Difference behaviors in differenct situations, this is a little complex.
I know the purpose is to check deadlock, but it means that it should always check the return value when locking, and

Code: Select all

if (deadlock is detected)
{
    // no efficient procedure here, instead re-coding is needed
}
Thanks
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxMessageQueue::Post never blocks?

Post by ONEEYEMAN »

Hi,
If you don't use MT the point is mute.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMessageQueue::Post never blocks?

Post by doublemax »

Right, blocks in difference threads. Difference behaviors in differenct situations, this is a little complex.
I can't think of a scenario where using wxMessageQueue() can create a deadlock. Not even if i tried to create one on purpose.
Use the source, Luke!
Post Reply