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


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

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

相关内容