windowsmultithreadingdelphiwinapiproducer-consumer

How to keep UI responsive when consuming items produced by background thread producer?


Short Version

Any message you post is always higher priority than WM_PAINT, WM_MOUSEMOVE, WM_KEYDOWN—causing the application be become unresponsive.

Long Version

I've offloaded a long-running, synchronous, operation to a background thread. It takes a while to get going, but eventually it starts producing items very nicely.

The question is then how to consume then - while maintaining a responsive UI (i.e. responding to Paint and UserInput messages).

One lock-free example sets up a while loop; we consume items while they are items to consume:

// You call this function when the consumer receives the
// signal raised by WakeConsumer().
void ConsumeWork()
{
   Thing item;
   while ((item = InterlockedGetItemOffTheSharedList(sharedList)) != nil)
   {
      ConsumeTheThing(item);
   }
}

The problem is that the background thread, once it gets going, can produce the items very quickly. This means that my while loop will never have a chance to stop. That means it will never go back to the message queue to respond to pending paint and mouse input events.

I've turned my asynchronous multi-threaded application in a synchronous wait as it sits inside:

while (StuffToDo)
{
   Consume(item);
}

Posting Messages

Another idea is to have the background thread PostMessage a message to the main thread every time an item is available:

ProduceItemsThreadMethod()    
{
   Preamble();
   while (StuffToProduce())
   {
       Thing item = new Item();
       SetupTheItem(item);
       InterlockedAddItemToTheSharedList(item);
       PostMessage(hwndMainThreadListener, WM_ItemReady, 0, 0);
   }
}  

The problem with this is that any posted message is always higher priority than any:

So as long as there is posted messages available, my application will not be responding to paint and input messages.

while GetMessage(out msg)
{
   DispatchMessage(out msg);
}

Every call to GetMessage will return a fresh WM_ItemReady message. My Windows message processing will be flooded with ItemReady messages - preventing me from processing paints until all the items have been added.

I've turned my asynchronous multi-threaded application in a synchronous wait.

Limiting the number of posted messages doesn't help

The above is actually worse than the first variation, because we flood the main thread with posted messages. What we want to do is only post a message if the main thread hasn't dealt with the previous message we posted. We can create a flag that is used to indicate if we've already posted a message, and if the main thread still hasn't processed it

ProduceItemsThreadMethod()    
{
   Preamble();
   while (StuffToProduce())
   {
       Thing item = new Item();
       SetupTheItem(item);
       InterlockedAddItemToTheSharedList(item);

       //Only post a message if the main thread has a message waiting
       int oldFlagValue = Interlocked.Exchange(g_ItemsReady, 1);
       if (oldFlagValue == 0) 
           PostMessage(hwndMainThreadListener, WM_ItemReady, 0, 0);
   }
}  

And in the main thread we clear the "ItemsReady" flag when we've processed the queued items:

void ConsumeWork()
{
   Thing item;
   while ((item = InterlockedGetItemOffTheSharedList(sharedList)) != nil)
   {
      ConsumeTheThing(item);
   }

   Interlocked.Exchange(g_ItemsReady, 0); //tell the thread it can post messages to us again
}

The problem again is that the thread can fill the list faster than we can consume it; so we never get a chance to fall out of the ConsumeWork() function in order to handle user input.

As soon as ConsumeWork returns, the background producer thread generates a new WM_ItemReady message. The very next time i call GetMessage

while GetMessage(out msg)
{
   DispatchMessage(out msg);
}

it will be a WM_itemReady message. I will be stuck in a loop.

I've turned my asynchronous multi-threaded application in a synchronous wait.

Limiting ourselves to a count of items doesn't help

We could try forcing a break out of the while loop after, say, processing 100 items:

void ConsumeWork()
{
   int itemsProcessed = 0;
   Thing item;
   while ((item = InterlockedGetItemOffTheSharedList(sharedList)) != nil)
   {
      ConsumeTheThing(item);
      itemsProcessed += 1;
      if (itemsProcessed >= 250)
         break;
   }

   Interlocked.Exchange(g_ItemsReady, 0); //tell the thread it can post messages to us again
}

This suffers from the same problem as the previous incarnation. Although we will leave the while loop, the very next message we will recieve will again be the WM_ItemReady:

while (GetMessage(...) != 0)
{
   TranslateMessge(...);
   DispatchMessage(...);
}

that's because WM_PAINT messages will only appear if there are no other messages. And the thread is itching to create a new WM_ItemReady message and post it in my queue.

Pumping the message loop myself?

Some people cry a little inside when they see people manually pumping messages to fix unresponsive applications. So lets try manually pumping messages to fix unresponsive applications!

void ConsumeWork()
{
   Thing item;
   while ((item = InterlockedGetItemOffTheSharedList(sharedList)) != nil)
   {
      ConsumeTheThing(item);
      ManuallyPumpPaintAndInputEvents();
   }

   Interlocked.Exchange(g_ItemsReady, 0); //tell the thread it can post messages to us again
}

I won't go into the details of that function, because it leads to the re-entrancy problem. If the user of my library happens to try to close the window they're on, destroying my helper class with it, i will suddenly come back to execution inside a class that has been destroyed:

ConsumeTheThing(item);
ManuallyPumpPaintAndInputEvents(); //processes WM_LBUTTONDOWN messages will closes the window which destroys me
InterlockedGetItemOffTheSharedList(sharedList) //sharedList no longer exist BOOM

Down and down I go

I keep going in circles trying to solve the problem of how to maintain a responsive UI when using background threads. I've tinkered with four solutions in this question, and three others before asking it.

I can't be the first person to have used the Producer-Consumer model in a user interface.

How do you maintain a responsive UI?

If only there was a way to post a message with priority lower than Paint, Input, and Timer ☹️


Solution

  • The answer is to use a timer.

    I've known this for about 17 years, when dual-cores first became a thing, and multi-threaded code suddenly became synchronous because the main thread can't get anything done because we used to only send a notification after the main thread had processed the last notification. And since there is no "message" you can post that goes "to the back of the line", the main thread just locks up.

    And about 8 years ago I was hoping to find the correct way to write a producer-consumer on Windows.

    Turns out there is no correct way; you just have to hack it.