读书人

新手提问:关于 消息死锁解决方法

发布时间: 2012-01-18 00:23:26 作者: rapoo

新手提问:关于 消息死锁
今日看MSDN学习SDK,发现了一个 Message Deadlocks 的主题。
贴出原文
Message Deadlocks
A thread that calls the SendMessage function to send a message to another thread cannot continue executing until the window procedure that receives the message returns. If the receiving thread yields control while processing the message, the sending thread cannot continue executing, because it is waiting for SendMessage to return. If the receiving thread is attached to the same queue as the sender, it can cause an application deadlock to occur. (Note that journal hooks attach threads to the same queue.)

Note that the receiving thread need not yield control explicitly; calling any of the following functions can cause a thread to yield control implicitly.


看了一下,意思是说 如果一个线程用SendMessage函数给其它线程发消息,如果接收线程在处理消息,那么发送线程就无法继续执行下去,因为发送线程调用的SendMessage函数一直都无法返回。


我就觉得奇怪。win中不是有消息队列一说么?SendMessage只要把消息扔到 接收线程 的消息队列里就可以了吧,然后接收线程那头用while循环 + GetMessage一个一个的处理消息队列里的消息就行了,为什么还会导致SendMessage一直无法返回??

哪位给解释下?谢谢 另外yield control 应该怎么翻译啊,词霸查不到合适的解释,呵呵

[解决办法]
SendMessage()函数被调用时并不会立即返回,需要等到发送的消息处理完毕后才返回。即发送的消息在处理过程中时,该函数处于挂起状态。这只是我的理解,不知对不对。
[解决办法]
楼上说得对,SendMessage并不是将消息发给目标线程的消息队列,它是直接调用目标窗口听WndProc,所以一定要等到对方的窗口处理程序处理完消息才能返回,将消息发给目标线程的消息队列的API是:PostMessage,所以PostMessage不会暂停,发送后直接返回

读书人网 >VC/MFC

热点推荐