b bst td

1.向线程PostMessage
  函数controlled_module_ex::postmessage完成消息推送。
  虚拟函数controlled_module_ex::message(const _command & cmd)实现消息接收。

  1. #include "controlled_module_ex.hpp" 
  2. class thdex: public controlled_module_ex 
  3. protected
  4.     virtual void message(const _command & cmd) 
  5.     { 
  6.         controlled_module_ex::message(cmd); 
  7.         if(cmd.nCmd==BM_USER+1) 
  8.         { 
  9.             cout << "get message" << endl; 
  10.         } 
  11.     } 
  12. }; 
  13.  
  14. int _tmain(int argc, _TCHAR* argv[]) 
  15.     thdex t; 
  16.     t.safestart(); 
  17.     t.postmessage(BM_USER+1); 
  18.     char buf[10]; 
  19.     gets_s(buf,sizeof buf); 
  20.     t.safestop(); 
  21.     return 0; 

2.向线程PostMessage,并携带简单对象参数
我们都知道常规的PostMessage要传参,如果是整型参数,还可以用强制转换,但如果是其他类型,例如字符串,我们就必须创建一个字符串缓冲,把缓冲指针作为参数传过去,线程还不能忘记删除,否则导致内存泄漏,自定义结构也是一样的操作,如果想尝试传递一个CString对象,是不可能的。
幸运的是boost提供了boost::any来抽象任何对象类型,controlled_module_ex的消息传递都是基于boost::any来完成,程序员可以由此写出干净而且内存安全的代码。

 
  1. #include "controlled_module_ex.hpp" 
  2.  
  3. struct mystruct 
  4.     string a; 
  5.     int b; 
  6. }; 
  7. class thdex: public controlled_module_ex 
  8. protected
  9.     virtual void message(const _command & cmd) 
  10.     { 
  11.         controlled_module_ex::message(cmd); 
  12.         if(cmd.nCmd==BM_USER+1) 
  13.         { 
  14. cout << "get integer:" << boost::any_cast<int>(cmd.anyParam) << endl; 
  15.         } 
  16.         if(cmd.nCmd==BM_USER+2) 
  17.         { 
  18. cout << "get string:" << boost::any_cast<string>(cmd.anyParam) << endl; 
  19.         } 
  20.         if(cmd.nCmd==BM_USER+3) 
  21.         { 
  22. mystruct par = boost::any_cast<mystruct>(cmd.anyParam); 
  23. cout << "get mystruct:" << par.a << "," << par.b << endl; 
  24.         } 
  25.     } 
  26. }; 
  27.  
  28. int _tmain(int argc, _TCHAR* argv[]) 
  29.     thdex t; 
  30.     t.safestart(); 
  31.     t.postmessage(BM_USER+1,123); 
  32.     t.postmessage(BM_USER+2,string("hello world")); 
  33.     mystruct par; 
  34.     par.a = "hello world"
  35.     par.b = 123; 
  36.     t.postmessage(BM_USER+3,par); 
  37.  
  38.     char buf[10]; 
  39.     gets_s(buf,sizeof buf); 
  40.     t.safestop(); 
  41.     return 0; 

3.向线程PostMessage,并传递内存块参数
假如我们书写了一个录音子线程,如何将录制的语音数据传递给其他线程呢,常规做法是创建一个缓冲,将语音数据填充进去,然后将缓冲地址作为参数传递,这种做法要求接收线程不能忘记删除,否则会导致内存泄漏。
幸运的是boost提供了智能指针,可以类似java,c#的智能内存回收一样来管理内存分配,我们如果使用这个对象来作为参数传递,就可以完美的防范内存泄漏行为,就算子线程没有处理,别担心,内存它会自动回收的。

 
  1. #include "controlled_module_ex.hpp" 
  2.  
  3. struct mystruct 
  4.     boost::shared_ptr<char> data; 
  5.     int datalen; 
  6. }; 
  7. class thdex: public controlled_module_ex 
  8. protected
  9.     virtual void message(const _command & cmd) 
  10.     { 
  11.         controlled_module_ex::message(cmd); 
  12.         if(cmd.nCmd==BM_USER+1) 
  13.         { 
  14. cout << "get sharedptr" << endl; //仅仅得到数据,得不到数据长度 
  15.         } 
  16.         if(cmd.nCmd==BM_USER+2) 
  17.         { 
  18. mystruct par = boost::any_cast<mystruct>(cmd.anyParam); 
  19. cout << "get sharedptr len:" << par.datalen << endl; 
  20.         } 
  21.     } 
  22. }; 
  23.  
  24. int _tmain(int argc, _TCHAR* argv[]) 
  25.     thdex t; 
  26.     t.safestart(); 
  27.     t.postmessage(BM_USER+1,boost::shared_ptr<char>(new char[1000])); 
  28.     mystruct par; 
  29.     par.datalen = 1000; 
  30.     par.data = boost::shared_ptr<char>(new char[par.datalen]); 
  31.     t.postmessage(BM_USER+2,par); 
  32.  
  33.     char buf[10]; 
  34.     gets_s(buf,sizeof buf); 
  35.     t.safestop(); 
  36.     return 0; 

3.向线程SendMessage

函数controlled_module_ex::execute完成这个工作
虚拟函数controlled_module_ex::on_command(const unsigned int command,const boost::any par)响应消息处理

 
  1. #include "controlled_module_ex.hpp" 
  2.  
  3. class thdex: public controlled_module_ex 
  4. protected
  5.     boost::any on_command(const unsigned int command,const boost::any par) 
  6.     { 
  7.         if(command==1) 
  8.         { 
  9.             cout << "on command" << endl; 
  10.             return 0; 
  11.         } 
  12.         if(command==2) 
  13.         { 
  14.             cout << "on command,par:" << boost::any_cast<string>(par) << endl; 
  15.             return 0; 
  16.         } 
  17.         if(command==3) 
  18.         { 
  19.             return true
  20.         } 
  21.         else 
  22.             return controlled_module_ex::on_command(command,par); 
  23.     } 
  24. }; 
  25.  
  26. int _tmain(int argc, _TCHAR* argv[]) 
  27.     thdex t; 
  28.     t.safestart(); 
  29.     t.execute(1,0);//等待子线程处理完成 
  30.     t.execute(2,string("hello world"));//带参数 等待子线程完成 
  31.     bool rs = boost::any_cast<bool>(t.execute(3,0));//等待子线程处理完成,并取得返回值 
  32.     cout << "get thread result:" << rs << endl; 
  33.     boost::any timeout = t.execute(4,0,1000);//等待子线程处理,超时1秒 
  34.     if(timeout.empty()) 
  35.         cout << "timeout " << endl; 
  36.  
  37.     char buf[10]; 
  38.     gets_s(buf,sizeof buf); 
  39.     t.safestop(); 
  40.     return 0; 

4.定时器
类似于CWnd::OnTimer,controlled_module_ex也提供一个虚拟函数virtual void on_timer(const controlled_timer * p);来处理定时

 
  1. #include "controlled_module_ex.hpp" 
  2. class thdex: public controlled_module_ex 
  3. protected
  4.     virtual void on_timer(const controlled_timer *p) 
  5.     { 
  6.         cout << "ontimer" << endl; 
  7.     } 
  8. }; 
  9.  
  10. int _tmain(int argc, _TCHAR* argv[]) 
  11.     thdex t; 
  12.     controlled_timer timer; 
  13.     t.safestart(); 
  14.     timer.starttimer(1000,&t); 
  15.  
  16.     char buf[10]; 
  17.     gets_s(buf,sizeof buf); 
  18.     t.safestop(); 
  19.     return 0; 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值