sprint 1-alpha
|
00001 /****************************************************************************** 00002 * SPRINT::thread_group 00003 * 00004 * Copyright (C) 2011 Paolo Medici <www.pmx.it> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00027 #ifndef _THREAD_POOL_H 00028 #define _THREAD_POOL_H 00029 00030 #include <sprint/thread/detail/function.h> 00031 #include <vector> 00032 00033 namespace sprint { 00034 00036 #ifdef WIN32 00037 class thread_group { 00038 00039 std::vector<HANDLE> m_threads; 00040 00041 private: 00043 thread_group(const thread_group & src) { } 00044 00045 public: 00046 00047 thread_group() { } 00048 00050 ~thread_group() 00051 { 00052 for(std::vector<HANDLE>::const_iterator i = m_threads.begin(); i!= m_threads.end(); ++i) 00053 ::CloseHandle(*i); 00054 } 00055 00057 bool create_thread(const sprint::thread_function & p) 00058 { 00059 HANDLE hThread; 00060 DWORD dwThreadId; 00061 hThread = CreateThread(NULL, 00062 0, 00063 p.proc, 00064 p.param, 00065 0, 00066 &dwThreadId); 00067 if(hThread != INVALID_HANDLE_VALUE) 00068 m_threads.push_back(hThread); 00069 return hThread != INVALID_HANDLE_VALUE; 00070 } 00071 00072 00074 void join_all() 00075 { 00076 ::WaitForMultipleObjects(m_threads.size(), &m_threads[0], TRUE, INFINITE); 00077 } 00078 00079 }; 00080 #else 00081 00082 00083 #endif 00084 00085 } 00086 00087 #endif 00088