开发者

Priority between thread messages?

Though I开发者_JAVA技巧 am quoting this in context of Borland C++ application, but the question is both specific for Borland as well as generic in nature.

In a Borland C++ project, I observe that a user interaction with the GUI (say a menu item click) is taking less priority than a task delegated to the main thread using Synchronize(), even though the user interaction is happening a few milliseconds earlier. When the main thread is available, the delegated task is done first and then the action corresponding to the user interaction is performed. A worker thread delegates a task to main thread and waits for the task to complete using Synchronize(). So we can equate Synchronize() to SendMessage().

I believe that the user interaction queues up as a message in the message queue and same should be the case with the delegated task. But how does the task execute first? Is there any priority between messages?


Up to and including C++Builder 5, Synchronize() did indeed make a call to SendMessage(). But in C++Builder 6, Synchronize() was re-written to not use SendMessage() anymore (to support Linux under CLX). Requests are now placed in a FIFO queue, and the VCL periodically calls CheckSynchronize() to process the queue. Even though CLX is long dead, Synchronize() still uses the same FIFO queue (and it has been enhanced over the years).

Aside from that, in cases where SendMessage() is used, it does have higher priority. User interactions are posted messages to the main thread message queue (aka PostMessage()). Although SendMessage() goes directly to a window's wndproc, it is not called until the receiving window's owning thread performs message processing if sent by a different thread (which used to be the case with Synchronize()). Pending SendMessage() requests to the main thread message queue have higher priority than pending posted messages to the same queue, as there are other threads/processes being blocked until the pending SendMessage() requests are processed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜