中国开发网: 论坛: 程序员情感CBD: 贴子 275069
周星驰
“将数据类的指针用发消息的办法发给另外一个线程”是个非常不正确的做法。
跨线程发消息,如果sendmessage就可能阻塞,如果postmessage就可能失效,具体的可查阅msdn,摘抄一小部分这里:
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.)

其实最可靠的做法是:如果你的数据是通过工作者线程处理的,当有数据变换时,不应该把变换的数据直接传给界面,而是发一个通知给界面(postmessage),告诉界面数据变换了,即一些附加自定义信息数据就可以了,一定不能是什么指针之类的(及时非栈上的),因为根本就不能控制这个指针会被什么时候处理或会否处理,让界面自个儿来取更新的数据同步其自身的数据备份或刷新界面等。不同工作者线程间则可以通过一个信号体等等来通知。更有如果你用postthreadmessage之类的,那么可能你会碰到当postthreadmessage失败时需要调用sleep,呵呵,也查阅msdn吧。

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录