1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
3 // Copyright (c) 2006 Peter K�mmel
4 // Permission to use, copy, modify, distribute and sell this software for any
5 //     purpose is hereby granted without fee, provided that the above copyright
6 //     notice appear in all copies and that both that copyright notice and this
7 //     permission notice appear in supporting documentation.
8 // The author makes no representations about the
9 //     suitability of this software for any purpose. It is provided "as is"
10 //     without express or implied warranty.
11 ////////////////////////////////////////////////////////////////////////////////
12 #ifndef LOKI_THREAD_H_
13 #define LOKI_THREAD_H_
14 
15 // $Id: Thread.h 761 2006-10-17 20:48:18Z syntheticpp $
16 
17 #include <loki/Threads.h>
18 #include <vector>
19 
20 #if defined(_WIN32)
21 
22     #include <process.h>
23 
24     typedef unsigned int (WINAPI*ThreadFunction_)(void *);
25 
26     #define LOKI_pthread_t HANDLE
27 
28     #define LOKI_pthread_create(handle,attr,func,arg) \
29         (int)((*handle=(HANDLE) _beginthreadex (NULL,0,(ThreadFunction_)func,arg,0,NULL))==NULL)
30 
31     #define LOKI_pthread_join(thread) \
32         ((WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
33 
34 #else
35 
36       #define LOKI_pthread_t \
37                  pthread_t
38     #define LOKI_pthread_create(handle,attr,func,arg) \
39                  pthread_create(handle,attr,func,arg)
40     #define LOKI_pthread_join(thread) \
41                  pthread_join(thread, NULL)
42 
43 #endif
44 
45 namespace Loki
46 {
47 
48 
49     ////////////////////////////////////////////////////////////////////////////////
50     //  \class Thread
51     //
52     //  \ingroup ThreadingGroup
53     //  Very simple Thread class
54     ////////////////////////////////////////////////////////////////////////////////
55     class Thread
56     {
57     public:
58 
59         typedef void* (*ThreadFunction)(void *);
60 
Thread(ThreadFunction func,void * parm)61         Thread(ThreadFunction func, void* parm)
62         {
63             func_ = func;
64             parm_ = parm;
65         }
66 
start()67         int start()
68         {
69             return LOKI_pthread_create(&pthread_, NULL, func_, parm_);
70         }
71 
WaitForThread(const Thread & thread)72         static int WaitForThread(const Thread& thread)
73         {
74             return LOKI_pthread_join(thread.pthread_);
75         }
76 
JoinThreads(const std::vector<Thread * > & threads)77         static void JoinThreads(const std::vector<Thread*>& threads)
78         {
79             for(size_t i=0; i<threads.size(); i++)
80                 WaitForThread(*threads.at(i));
81         }
82 
DeleteThreads(std::vector<Thread * > & threads)83         static void DeleteThreads(std::vector<Thread*>& threads)
84         {
85             for(size_t i=0; i<threads.size(); i++)
86             {
87                 delete threads.at(i);
88             }
89             threads.clear();
90         }
91 
92     private:
93         LOKI_pthread_t pthread_;
94         ThreadFunction func_;
95         void* parm_;
96     };
97 
98 
99 
100 
101 } // namespace Loki
102 
103 #endif
104 
105