X-Boost  2.3.8
thread_group.h
Go to the documentation of this file.
1 /******************************************************************************
2 * SPRINT::thread_group
3 *
4 * Copyright (C) 2011 Paolo Medici <www.pmx.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *******************************************************************************/
21 
27 #ifndef _THREAD_POOL_H
28 #define _THREAD_POOL_H
29 
30 #include "function.h"
31 #include <vector>
32 
33 namespace sprint {
34 
36 #ifdef WIN32
37  class thread_group {
38 
39  std::vector<HANDLE> m_threads;
40 
41  private:
43  thread_group(const thread_group & src) { }
44 
45  public:
46 
47  thread_group() { }
48 
51  {
52  for(std::vector<HANDLE>::const_iterator i = m_threads.begin(); i!= m_threads.end(); ++i)
53  ::CloseHandle(*i);
54  }
55 
58  {
59  HANDLE hThread;
60  DWORD dwThreadId;
61  hThread = CreateThread(NULL,
62  0,
63  p.proc,
64  p.param,
65  0,
66  &dwThreadId);
67  if(hThread != INVALID_HANDLE_VALUE)
68  m_threads.push_back(hThread);
69  return hThread != INVALID_HANDLE_VALUE;
70  }
71 
72 
74  void join_all()
75  {
76  ::WaitForMultipleObjects(m_threads.size(), &m_threads[0], TRUE, INFINITE);
77  }
78 
79  };
80 #else
81 
82  class thread_group {
83 
84  std::vector<pthread_t> m_threads;
85 
86  private:
88  thread_group(const thread_group & src) { }
89 
90  public:
91 
92  thread_group() { }
93 
96  {
97 // for(std::vector<HANDLE>::const_iterator i = m_threads.begin(); i!= m_threads.end(); ++i)
98 // ::CloseHandle(*i);
99  }
100 
103  {
104  pthread_t thread;
105  int ret = pthread_create(&thread, 0,
106  p.proc,
107  p.param);
108  m_threads.push_back(thread);
109  return ret==0;
110  }
111 
112 
114  void join_all()
115  {
116  for(std::vector<pthread_t>::iterator i = m_threads.begin(); i!=m_threads.end(); ++i)
117  {
118  pthread_join(*i,0);
119  }
120  }
121 
122  };
123 
124 
125 #endif
126 
127 }
128 
129 #endif
130 
pthread thread creation params
Definition: function.h:95
Definition: thread.h:170
Definition: thread_group.h:82
void join_all()
wait all threads terminate
Definition: thread_group.h:114
bool create_thread(const sprint::thread_function &p)
create an additional thread
Definition: thread_group.h:102
~thread_group()
release memory associated to thread
Definition: thread_group.h:95