大胖的部落格

Just a note

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  112 随笔 :: 0 文章 :: 3 评论 :: 0 Trackbacks
1、全局变量
需要用volatile关键字声明。

bool volatile bFlag = false;


2、事件内核对象
通过对Event对象设置是否受信,来通知其它线程函数是否执行。


3、通过PostThreadMessage(),可以根据线程ID将消息传递给目标线程,前提是目标线程必须要有消息队列。
#include <iostream>
#include 
<windows.h>
#include 
<process.h>
using namespace std;


#define WM_TEST_MSG    0x1555

//线程函数
unsigned int __stdcall ThreadProc(void* )
{
    MSG   msg;   
    
//从该线程的消息队列里取消息
    while(::GetMessage(&msg,   NULL,   0,   0))   
    
{   
        
switch(msg.message)   
        
{   
        
case WM_TEST_MSG:
            cout
<<"get"<<endl;
            
break;

        
default:
            
break;
        }

    }


    
return 0;
}


int main()
{
    
//启动线程,保存线程ID
    UINT iThreadID = 0;
    ::_beginthreadex(NULL, 
0, ThreadProc, NULL, 0&iThreadID);

    
while(!getchar())
    
{

    }

    
    
//通过线程ID向目标线程发消息
    ::PostThreadMessage(iThreadID, WM_TEST_MSG, 00);

    
while(!getchar())
    
{

    }

    
return 0;
}


posted on 2009-06-16 16:40 大胖 阅读(116) 评论(0)  编辑 收藏 引用 所属分类: Win32

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理