Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ThreadManager.h"
- #include <iostream>
- #include <string>
- using namespace std;
- // MyThreadFunction - outputs that it works, and ends
- DWORD WINAPI MyThreadFunction( LPVOID lpParam )
- {
- int i = (int)lpParam;
- cout << i << ".) I work! " << endl;
- return 0;
- }
- // MyThreadFunction2 - loops continuously forever, outputing "working..." every 1sec
- DWORD WINAPI MyThreadFunction2( LPVOID lpParam )
- {
- int i = (int)lpParam;
- while(1) {
- cout << i << ".) Working... " << endl;
- Sleep(1);
- }
- return 0;
- }
- // main - tests different cases while using threads
- int main(int argc, char * argv[]) {
- ThreadManager *tm = new ThreadManager();
- tm->terminateAllThreads();
- cout << "Creating ten threads" << endl;
- for (int i = 1; i <= 10; i++) {
- tm->startThread(MyThreadFunction, (LPVOID) i);
- Sleep(10);
- }
- tm->terminateAllThreads();
- delete tm;
- tm = NULL;
- ThreadManager tm2;
- tm2.terminateAllThreads();
- tm2.startThread(MyThreadFunction2, (LPVOID) 10);
- Sleep(10);
- tm2.terminateAllThreads();
- cout << "Good Bye." << endl;
- string temp;
- cin >> temp;
- return 0;
- }
- DWORD WINAPI ThreadManager::updateLoop(LPVOID lpParam)
- {
- ThreadManager *ptr = (ThreadManager *)lpParam;
- while(1) {
- DWORD threadStatus;
- CSingleLock singleLock(&ptr->m_CritSection);
- singleLock.Lock(); // Attempt to lock the shared resource
- if (singleLock.IsLocked())
- {
- // Iterate through the threads list<HANDLE>
- // Remove any HANDLE(s) which GetExitCodeThread != STILL_ACTIVE
- for (int i = 0; i < ptr->threads.size(); i++) {
- if (GetExitCodeThread(ptr->threads.at(i), &threadStatus) && threadStatus != STILL_ACTIVE) {
- CloseHandle(ptr->threads.at(i));
- // Delete HANDLE
- cout << "Deleting HANDLE:" << ptr->threads.at(i) << endl;
- ptr->threads.erase(ptr->threads.begin()+i);
- i--; // Decrement var (i) to not skip next element in list
- }
- }
- }
- // unlock the resource for others
- singleLock.Unlock();
- // sleep for short interfval, as it doesn't need to be run too often.
- Sleep(400);
- }
- return (DWORD)NULL;
- }
- ThreadManager::ThreadManager(int max)
- {
- // Assign maxThreads to max value
- maxThreads = max; // FIXME: max threads not implemented..
- // Start updateThread, and let it run updateLoop() until terminated
- updateThread = CreateThread(
- NULL, // default security attributes
- 0, // use default stack size
- updateLoop, // thread function name
- this, // arugument to thread function
- 0, // use default creation flag
- NULL); // ignore thread identifier
- // Check the return value for success
- // If failed, exit process.
- if (updateThread == NULL) {
- ExitProcess(0);
- }
- }
- ThreadManager::~ThreadManager()
- {
- // Terminate the updateThread loop
- TerminateThread(updateThread, 0);
- // Release handle to updateThread
- CloseHandle(updateThread);
- // Terminate all existing threads
- terminateAllThreads();
- }
- void ThreadManager::startThread(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter)
- {
- CSingleLock singleLock(&m_CritSection);
- singleLock.Lock(); // Attempt to lock the shared resource
- if (singleLock.IsLocked())
- {
- // Add and create new thread HANDLE to threads list
- threads.push_back(CreateThread(
- NULL, // default security attributes
- 0, // use default stack size
- lpStartAddress, // thread function name
- lpParameter, // arugument to thread function
- 0, // use default creation flag
- NULL)); // ignore thread identifier.
- }
- // unlock the resource for others
- singleLock.Unlock();
- }
- void ThreadManager::terminateAllThreads()
- {
- CSingleLock singleLock(&m_CritSection);
- singleLock.Lock(); // Attempt to lock the shared resource
- if (singleLock.IsLocked()) // Resource has been locked
- {
- cout << "ENTERED" << endl;
- // loop through threads list,
- // terminate and close handles to each
- for (int i = 0; i < threads.size(); i++) {
- HANDLE thread = threads.back();
- threads.pop_back();
- cout << "CURRENT LOC: " << i << "; HANDLE=" << thread << "; ";
- cout << "STAT: " << TerminateThread(thread, 0) << endl;
- cout << "Terminated: " << thread << endl;
- CloseHandle(thread);
- }
- cout << "EXITED" << endl;
- }
- singleLock.Unlock();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement