1.向线程PostMessage
函数controlled_module_ex::postmessage完成消息推送。
虚拟函数controlled_module_ex::message(const _command & cmd)实现消息接收。
- #include "controlled_module_ex.hpp"
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_USER+1)
- {
- cout << "get message" << endl;
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.postmessage(BM_USER+1);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
2.向线程PostMessage,并携带简单对象参数
我们都知道常规的PostMessage要传参,如果是整型参数,还可以用强制转换,但如果是其他类型,例如字符串,我们就必须创建一个字符串缓冲,把缓冲指针作为参数传过去,线程还不能忘记删除,否则导致内存泄漏,自定义结构也是一样的操作,如果想尝试传递一个CString对象,是不可能的。
幸运的是boost提供了boost::any来抽象任何对象类型,controlled_module_ex的消息传递都是基于boost::any来完成,程序员可以由此写出干净而且内存安全的代码。
- #include "controlled_module_ex.hpp"
- struct mystruct
- {
- string a;
- int b;
- };
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_USER+1)
- {
- cout << "get integer:" << boost::any_cast<int>(cmd.anyParam) << endl;
- }
- if(cmd.nCmd==BM_USER+2)
- {
- cout << "get string:" << boost::any_cast<string>(cmd.anyParam) << endl;
- }
- if(cmd.nCmd==BM_USER+3)
- {
- mystruct par = boost::any_cast<mystruct>(cmd.anyParam);
- cout << "get mystruct:" << par.a << "," << par.b << endl;
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.postmessage(BM_USER+1,123);
- t.postmessage(BM_USER+2,string("hello world"));
- mystruct par;
- par.a = "hello world";
- par.b = 123;
- t.postmessage(BM_USER+3,par);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
3.向线程PostMessage,并传递内存块参数
假如我们书写了一个录音子线程,如何将录制的语音数据传递给其他线程呢,常规做法是创建一个缓冲,将语音数据填充进去,然后将缓冲地址作为参数传递,这种做法要求接收线程不能忘记删除,否则会导致内存泄漏。
幸运的是boost提供了智能指针,可以类似java,c#的智能内存回收一样来管理内存分配,我们如果使用这个对象来作为参数传递,就可以完美的防范内存泄漏行为,就算子线程没有处理,别担心,内存它会自动回收的。
- #include "controlled_module_ex.hpp"
- struct mystruct
- {
- boost::shared_ptr<char> data;
- int datalen;
- };
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_USER+1)
- {
- cout << "get sharedptr" << endl; //仅仅得到数据,得不到数据长度
- }
- if(cmd.nCmd==BM_USER+2)
- {
- mystruct par = boost::any_cast<mystruct>(cmd.anyParam);
- cout << "get sharedptr len:" << par.datalen << endl;
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.postmessage(BM_USER+1,boost::shared_ptr<char>(new char[1000]));
- mystruct par;
- par.datalen = 1000;
- par.data = boost::shared_ptr<char>(new char[par.datalen]);
- t.postmessage(BM_USER+2,par);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
3.向线程SendMessage
函数controlled_module_ex::execute完成这个工作
虚拟函数controlled_module_ex::on_command(const unsigned int command,const boost::any par)响应消息处理
- #include "controlled_module_ex.hpp"
- class thdex: public controlled_module_ex
- {
- protected:
- boost::any on_command(const unsigned int command,const boost::any par)
- {
- if(command==1)
- {
- cout << "on command" << endl;
- return 0;
- }
- if(command==2)
- {
- cout << "on command,par:" << boost::any_cast<string>(par) << endl;
- return 0;
- }
- if(command==3)
- {
- return true;
- }
- else
- return controlled_module_ex::on_command(command,par);
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.execute(1,0);//等待子线程处理完成
- t.execute(2,string("hello world"));//带参数 等待子线程完成
- bool rs = boost::any_cast<bool>(t.execute(3,0));//等待子线程处理完成,并取得返回值
- cout << "get thread result:" << rs << endl;
- boost::any timeout = t.execute(4,0,1000);//等待子线程处理,超时1秒
- if(timeout.empty())
- cout << "timeout " << endl;
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
4.定时器
类似于CWnd::OnTimer,controlled_module_ex也提供一个虚拟函数virtual void on_timer(const controlled_timer * p);来处理定时
- #include "controlled_module_ex.hpp"
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void on_timer(const controlled_timer *p)
- {
- cout << "ontimer" << endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- controlled_timer timer;
- t.safestart();
- timer.starttimer(1000,&t);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }