歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

不定個數的C++函數指針

關於多線程下利用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. } 
Copyright © Linux教程網 All Rights Reserved