﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-星星点灯-随笔分类-Windows</title><link>http://www.cppblog.com/hongsion/category/5920.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 23 May 2008 17:47:05 GMT</lastBuildDate><pubDate>Fri, 23 May 2008 17:47:05 GMT</pubDate><ttl>60</ttl><item><title>关于windows、线程、消息的一点体会</title><link>http://www.cppblog.com/hongsion/archive/2008/01/02/40260.html</link><dc:creator>hongsion</dc:creator><author>hongsion</author><pubDate>Wed, 02 Jan 2008 11:35:00 GMT</pubDate><guid>http://www.cppblog.com/hongsion/archive/2008/01/02/40260.html</guid><wfw:comment>http://www.cppblog.com/hongsion/comments/40260.html</wfw:comment><comments>http://www.cppblog.com/hongsion/archive/2008/01/02/40260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hongsion/comments/commentRss/40260.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hongsion/services/trackbacks/40260.html</trackback:ping><description><![CDATA[<p>以下内容完全把window 操作系统当作一个黑盒，因此所有内容只能作为对其内部的一个猜测。<br><br>1. windows操作系统内部在创建一个线程的时候，会自动为它创建一个消息队列。<br><br>2.每当一个线程创建一个窗口的时候，操作系统内部都会把该窗口的Handle和线程相关联。很有可能在操作系统内部会维护一个窗口handle到线程的map. 还有一种可能就是窗口的成员变量里面有一个指针，指向创建它的线程。<br><br>3.窗口本身并没有消息队列，所有发到窗口的消息，都会自动被发到创建该窗口的线程的消息队列中。<br><br>4.每个线程只能处理自己线程队列里面的消息，不能处理其他线程消息队列里面的消息。<br>所以PeekMessage(LPMSG lpMsg, HWND hWnd, UINT,UINT,UINT)函数中，如果hWnd不是本线程创建的窗口，则该函数调用失败。<br><br>5.由于在线程消息队列里面的消息会包含有窗口句柄，所以PeekMessage可以专门处理某个特殊窗口的消息。</p>
<br>6. 曾经有疑问线程是不是只有创建了窗口才具有消息队列，但又觉得应该不是这样,因为在windows的API里面有个函数叫PostThreadMessage，可以直接把消息投递到线程的消息队列里面，而不需要任何窗口句柄。后来在MSDN里面有这么一段描述，觉得解释的很详细：<br>&#8220;The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread. To avoid the overhead of creating a message queue for non&#8211;GUI threads, all threads are created initially without a message queue. The system creates a thread-specific message queue only when the thread makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions".<br>&nbsp;这里唯一的疑问我想应该是&#8221;makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions", 这句话的意思是不是等同于创建一个窗口呢？ 
<img src ="http://www.cppblog.com/hongsion/aggbug/40260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hongsion/" target="_blank">hongsion</a> 2008-01-02 19:35 <a href="http://www.cppblog.com/hongsion/archive/2008/01/02/40260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>