不定个数的c++函数指针
来源:互联网 发布:做淘宝主图用什么软件 编辑:程序博客网 时间:2024/06/07 11:12
关于多线程下利用vector保存函数指针并调用的方法,这是一个最简单的函数指针调用实例,大家可以看到这样对于处理一些第一时间处理数据的业务非常合适,这样处理的实时性非常好,当然而且可以多个处理函数来处理数据,也可以将mytest这个class 封装起来,是外部使用的人不必关心内部如何产生数据的。外部直接
struct mystuct{int myInt;char myStr[100];};class mytest{private:static unsigned long WINAPI TestThread(LPVOID lpvoid); volatile unsigned long threadRun;int CreateTestThread();vector<void (*)(mystuct *pdata)> functionvector;HANDLE m_ExitEvent;public:mytest(void){}~mytest(void){}int AddFunctionAddr(void (*_ProcesseFunction)(mystuct *pdata));void initialize();void shutdown();};void mytest::initialize(){CreateTestThread();}void mytest::shutdown(){InterlockedExchange(&threadRun,0);WaitForSingleObject(m_ExitEvent,INFINITE);ResetEvent(m_ExitEvent);CloseHandle(m_ExitEvent);}int mytest::AddFunctionAddr(void (*_ProcesseFunction)(mystuct *pdata)){functionvector.push_back(_ProcesseFunction);return 0;}unsigned long WINAPI mytest::TestThread(LPVOID lpvoid){mytest* pMytest = (mytest*)lpvoid; int i =15;while(1){mystuct _mystuct;memset(_mystuct.myStr,0,100);memcpy(_mystuct.myStr,"月·小轩",sizeof("月·小轩"));_mystuct.myInt=i;if (0==pMytest->threadRun){break;}for (int i=0;i<(int)(pMytest->functionvector.size());i++){if (NULL!=(pMytest->functionvector[i])){(*(pMytest->functionvector[i]))(&_mystuct);}}i++;Sleep(1000);}SetEvent(pMytest->m_ExitEvent);return 0;}int mytest::CreateTestThread(){m_ExitEvent= CreateEvent(NULL,TRUE,FALSE,L"test_Eixt");InterlockedExchange(&threadRun,1);DWORD dwthreadID;HANDLE h_Handle= CreateThread(NULL,NULL,TestThread,this,0,&dwthreadID);if (NULL==h_Handle){CloseHandle(h_Handle);return 1;}CloseHandle(h_Handle);return 0;}void printFunction1(mystuct* data){int resultNumber = data->myInt;printf("%d\t",resultNumber);}void printFunction2(mystuct* data){printf("%s\t",data->myStr);}void main(){mytest* _mytest = new mytest();_mytest->AddFunctionAddr(printFunction1);_mytest->AddFunctionAddr(printFunction2);_mytest->initialize();system("pause");_mytest->shutdown();}
- 不定个数的c++函数指针
- 不定参数个数的C函数-
- 不定参数个数的C函数
- C语言中不定参数个数的函数
- 2.不定个数参数的函数写法
- 如何实现参数个数不定的函数?
- 传不定个数参数的函数写法
- c 不定参数的函数
- C语言不定参数个数,可变参数函数使用学习
- 用指针处理C语言中不定数目的函数参数
- 不定参数个数函数写法
- C++函数参数个数不定
- 函数中不定参数的处理(函数参数个数不定)
- C#如何实现不定参数个数的函数
- VC如何写参数个数不定的函数…
- Python给函数传递不定个数的参数
- C语言函数参数不定的例程
- C语言不定参数的函数
- 如何避免ViewController打开时Orientation错误
- Servlet生命周期与工作原理
- NoSQL对比:Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBase vs Membase vs Neo4j
- MySQL存储引擎
- Simple base conversion implementation use java
- 不定个数的c++函数指针
- air 移动开发配置文件的使用详细教程
- 图片切换效果
- REST调查资料整理
- 数据结构HASH总结三:实践基础篇
- 事物的隔离级别 isolation
- BT5 R2 下 Metasploit使用postgersql数据库
- 12c新特性,Pluggable Database
- air 设置ios应用程序属性