00001 /* 00002 * This file is part of ALVAR, A Library for Virtual and Augmented Reality. 00003 * 00004 * Copyright 2007-2012 VTT Technical Research Centre of Finland 00005 * 00006 * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi> 00007 * <http://www.vtt.fi/multimedia/alvar.html> 00008 * 00009 * ALVAR is free software; you can redistribute it and/or modify it under the 00010 * terms of the GNU Lesser General Public License as published by the Free 00011 * Software Foundation; either version 2.1 of the License, or (at your option) 00012 * any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, but WITHOUT 00015 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00016 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00017 * for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public License 00020 * along with ALVAR; if not, see 00021 * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>. 00022 */ 00023 00024 #include "Threads_private.h" 00025 00026 #include <vector> 00027 #include <windows.h> 00028 00029 namespace alvar { 00030 00031 struct StartThreadParameters 00032 { 00033 void *(*method)(void *); 00034 void *parameters; 00035 }; 00036 00037 static DWORD WINAPI startThread(void *parameters) 00038 { 00039 DWORD value; 00040 struct StartThreadParameters *p = (struct StartThreadParameters *)parameters; 00041 value = (DWORD)p->method(p->parameters); 00042 delete p; 00043 return value; 00044 } 00045 00046 class ThreadsPrivateData 00047 { 00048 public: 00049 ThreadsPrivateData() 00050 : mHandles() 00051 { 00052 } 00053 00054 std::vector<HANDLE> mHandles; 00055 }; 00056 00057 ThreadsPrivate::ThreadsPrivate() 00058 : d(new ThreadsPrivateData()) 00059 { 00060 } 00061 00062 ThreadsPrivate::~ThreadsPrivate() 00063 { 00064 for (int i = 0; i < (int)d->mHandles.size(); ++i) { 00065 CloseHandle(d->mHandles.at(i)); 00066 } 00067 d->mHandles.clear(); 00068 00069 delete d; 00070 } 00071 00072 bool ThreadsPrivate::create(void *(*method)(void *), void *parameters) 00073 { 00074 StartThreadParameters *p = new StartThreadParameters; 00075 p->method = method; 00076 p->parameters = parameters; 00077 00078 HANDLE thread = CreateThread( 00079 NULL, 00080 0, 00081 (LPTHREAD_START_ROUTINE)startThread, 00082 p, 00083 0, 00084 NULL); 00085 00086 if (thread != NULL) { 00087 d->mHandles.push_back(thread); 00088 return true; 00089 } 00090 return false; 00091 } 00092 00093 } // namespace alvar