Advertisement
Guest User

ThreadManager.cpp

a guest
Jan 11th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #include "ThreadManager.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // MyThreadFunction - outputs that it works, and ends
  8. DWORD WINAPI MyThreadFunction( LPVOID lpParam )
  9. {
  10.     int i = (int)lpParam;
  11.     cout << i << ".) I work! " << endl;
  12.     return 0;
  13. }
  14.  
  15. // MyThreadFunction2 - loops continuously forever, outputing "working..." every 1sec
  16. DWORD WINAPI MyThreadFunction2( LPVOID lpParam )
  17. {
  18.     int i = (int)lpParam;
  19.     while(1) {
  20.         cout << i << ".) Working... " << endl;
  21.         Sleep(1);
  22.     }
  23.     return 0;
  24. }
  25.  
  26. // main - tests different cases while using threads
  27. int main(int argc, char * argv[]) {
  28.     ThreadManager *tm = new ThreadManager();
  29.     tm->terminateAllThreads();
  30.     cout << "Creating ten threads" << endl;
  31.     for (int i = 1; i <= 10; i++) {
  32.         tm->startThread(MyThreadFunction, (LPVOID) i);
  33.         Sleep(10);
  34.     }
  35.     tm->terminateAllThreads();
  36.     delete tm;
  37.     tm = NULL;
  38.     ThreadManager tm2;
  39.     tm2.terminateAllThreads();
  40.     tm2.startThread(MyThreadFunction2, (LPVOID) 10);
  41.     Sleep(10);
  42.     tm2.terminateAllThreads();
  43.  
  44.     cout << "Good Bye." << endl;
  45.    
  46.     string temp;
  47.     cin >> temp;
  48.     return 0;
  49. }
  50.  
  51. DWORD WINAPI ThreadManager::updateLoop(LPVOID lpParam)
  52. {
  53.     ThreadManager *ptr = (ThreadManager *)lpParam;
  54.  
  55.     while(1) {
  56.         DWORD threadStatus;
  57.         CSingleLock singleLock(&ptr->m_CritSection);
  58.  
  59.         singleLock.Lock(); // Attempt to lock the shared resource
  60.         if (singleLock.IsLocked())
  61.         {
  62.             // Iterate through the threads list<HANDLE>
  63.             // Remove any HANDLE(s) which GetExitCodeThread != STILL_ACTIVE
  64.             for (int i = 0; i < ptr->threads.size(); i++) {
  65.                 if (GetExitCodeThread(ptr->threads.at(i), &threadStatus) && threadStatus != STILL_ACTIVE) {
  66.                     CloseHandle(ptr->threads.at(i));
  67.                     // Delete HANDLE
  68.                     cout << "Deleting HANDLE:" << ptr->threads.at(i) << endl;
  69.                     ptr->threads.erase(ptr->threads.begin()+i);
  70.                     i--; // Decrement var (i) to not skip next element in list
  71.                 }
  72.             }
  73.         }
  74.         // unlock the resource for others
  75.         singleLock.Unlock();
  76.         // sleep for short interfval, as it doesn't need to be run too often.
  77.         Sleep(400);
  78.     }
  79.     return (DWORD)NULL;
  80. }
  81.  
  82. ThreadManager::ThreadManager(int max)
  83. {
  84.     // Assign maxThreads to max value
  85.     maxThreads = max; // FIXME: max threads not implemented..
  86.  
  87.     // Start updateThread, and let it run updateLoop() until terminated
  88.     updateThread = CreateThread(
  89.                                 NULL,       // default security attributes
  90.                                 0,          // use default stack size
  91.                                 updateLoop, // thread function name
  92.                                 this,       // arugument to thread function
  93.                                 0,          // use default creation flag
  94.                                 NULL);      // ignore thread identifier
  95.  
  96.     // Check the return value for success
  97.     // If failed, exit process.
  98.     if (updateThread == NULL) {
  99.         ExitProcess(0);
  100.     }  
  101. }
  102.  
  103. ThreadManager::~ThreadManager()
  104. {
  105.     // Terminate the updateThread loop
  106.     TerminateThread(updateThread, 0);
  107.    
  108.     // Release handle to updateThread
  109.     CloseHandle(updateThread);
  110.    
  111.     // Terminate all existing threads
  112.     terminateAllThreads();
  113. }
  114.  
  115. void ThreadManager::startThread(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter)
  116. {
  117.     CSingleLock singleLock(&m_CritSection);
  118.  
  119.     singleLock.Lock(); // Attempt to lock the shared resource
  120.     if (singleLock.IsLocked())
  121.     {
  122.         // Add and create new thread HANDLE to threads list
  123.         threads.push_back(CreateThread(
  124.                                        NULL,           // default security attributes
  125.                                        0,              // use default stack size
  126.                                        lpStartAddress, // thread function name
  127.                                        lpParameter,    // arugument to thread function
  128.                                        0,              // use default creation flag
  129.                                        NULL));         // ignore thread identifier.
  130.     }
  131.     // unlock the resource for others
  132.     singleLock.Unlock();
  133. }
  134.  
  135. void ThreadManager::terminateAllThreads()
  136. {
  137.     CSingleLock singleLock(&m_CritSection);
  138.     singleLock.Lock(); // Attempt to lock the shared resource
  139.    
  140.     if (singleLock.IsLocked()) // Resource has been locked
  141.     {
  142.         cout << "ENTERED" << endl;
  143.         // loop through threads list,
  144.         // terminate and close handles to each
  145.         for (int i = 0; i < threads.size(); i++) {
  146.             HANDLE thread = threads.back();
  147.             threads.pop_back();
  148.             cout << "CURRENT LOC: " << i << "; HANDLE=" << thread << "; ";
  149.             cout << "STAT: " << TerminateThread(thread, 0) << endl;
  150.             cout << "Terminated: " << thread << endl;
  151.             CloseHandle(thread);
  152.         }
  153.         cout << "EXITED" << endl;
  154.     }
  155.     singleLock.Unlock();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement