手机版
你好,游客 登录 注册
背景:
阅读新闻

不定个数的C++函数指针

[日期:2012-11-24] 来源:Linux社区  作者:xueyunf [字体:大 中 小]

关于多线程下利用vector保存函数指针并调用的方法,这是一个最简单的函数指针调用实例,大家可以看到这样对于处理一些第一时间处理数据的业务非常合适,这样处理的实时性非常好,当然而且可以多个处理函数来处理数据,也可以将mytest这个class 封装起来,是外部使用的人不必关心内部如何产生数据的。外部直接

  1. struct mystuct 
  2. { 
  3.     int myInt; 
  4.     char myStr[100]; 
  5. }; 
  6.  
  7.  
  8. class mytest 
  9. { 
  10. private: 
  11.     static unsigned long WINAPI TestThread(LPVOID lpvoid);   
  12.  
  13.  
  14.     volatile unsigned long threadRun; 
  15.     int CreateTestThread(); 
  16.     vector<void (*)(mystuct *pdata)> functionvector; 
  17.     HANDLE m_ExitEvent; 
  18. public: 
  19.     mytest(void){} 
  20.     ~mytest(void){} 
  21.     int AddFunctionAddr(void (*_ProcesseFunction)(mystuct *pdata)); 
  22.     void initialize(); 
  23.     void shutdown(); 
  24.  
  25.  
  26. }; 
  27.  
  28.  
  29. void mytest::initialize() 
  30. { 
  31.     CreateTestThread(); 
  32. } 
  33. void  mytest::shutdown() 
  34. { 
  35.     InterlockedExchange(&threadRun,0); 
  36.     WaitForSingleObject(m_ExitEvent,INFINITE); 
  37.     ResetEvent(m_ExitEvent); 
  38.     CloseHandle(m_ExitEvent); 
  39. } 
  40. int mytest::AddFunctionAddr(void (*_ProcesseFunction)(mystuct *pdata)) 
  41. { 
  42.     functionvector.push_back(_ProcesseFunction); 
  43.     return 0; 
  44. } 
  45.  
  46.  
  47. unsigned long WINAPI  mytest::TestThread(LPVOID lpvoid) 
  48. { 
  49.     mytest* pMytest = (mytest*)lpvoid;   
  50.     int i =15; 
  51.     while(1) 
  52.     { 
  53.         mystuct _mystuct; 
  54.         memset(_mystuct.myStr,0,100); 
  55.         memcpy(_mystuct.myStr,"月·小轩",sizeof("月·小轩")); 
  56.         _mystuct.myInt=i; 
  57.  
  58.  
  59.         if (0==pMytest->threadRun) 
  60.         { 
  61.             break; 
  62.         } 
  63.  
  64.  
  65.         for (int i=0;i<(int)(pMytest->functionvector.size());i++) 
  66.         { 
  67.             if (NULL!=(pMytest->functionvector[i])) 
  68.             { 
  69.                 (*(pMytest->functionvector[i]))(&_mystuct); 
  70.             } 
  71.         } 
  72.         i++; 
  73.         Sleep(1000);     
  74.     } 
  75.     SetEvent(pMytest->m_ExitEvent); 
  76.     return 0; 
  77. } 
  78.  
  79.  
  80. int mytest::CreateTestThread() 
  81. { 
  82.     m_ExitEvent= CreateEvent(NULL,TRUE,FALSE,L"test_Eixt"); 
  83.     InterlockedExchange(&threadRun,1); 
  84.  
  85.  
  86.  
  87.  
  88.     DWORD dwthreadID; 
  89.     HANDLE h_Handle= CreateThread(NULL,NULL,TestThread,this,0,&dwthreadID); 
  90.     if (NULL==h_Handle) 
  91.     { 
  92.         CloseHandle(h_Handle); 
  93.         return 1; 
  94.     }     
  95.     CloseHandle(h_Handle); 
  96.     return 0; 
  97. } 
  98.  
  99.  
  100. void printFunction1(mystuct* data) 
  101. { 
  102.     int resultNumber = data->myInt; 
  103.     printf("%d\t",resultNumber); 
  104. } 
  105. void printFunction2(mystuct* data) 
  106. { 
  107.     printf("%s\t",data->myStr); 
  108. } 
  109.  
  110.  
  111. void main() 
  112. { 
  113.     mytest* _mytest = new mytest(); 
  114.     _mytest->AddFunctionAddr(printFunction1); 
  115.     _mytest->AddFunctionAddr(printFunction2); 
  116.     _mytest->initialize(); 
  117.  
  118.  
  119.     system("pause"); 
  120.     _mytest->shutdown(); 
  121. } 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款