Rev 969 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 969 | Rev 983 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | * Copyright (C) November 29, 2008 by Mattias Runge * |
2 | * Copyright (C) November 29, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * thread.h * |
4 | * thread.h * |
| 5 | * * |
5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
9 | * (at your option) any later version. * |
| 10 | * * |
10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
14 | * GNU General Public License for more details. * |
| 15 | * * |
15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | 21 | ||
| 22 | #ifndef _THREAD_H |
22 | #ifndef _THREAD_H |
| 23 | #define _THREAD_H |
23 | #define _THREAD_H |
| 24 | 24 | ||
| 25 | #include <pthread.h> |
25 | #include <pthread.h> |
| 26 | 26 | ||
| 27 | template <class T> |
27 | template <class T> |
| 28 | class Thread |
28 | class Thread |
| 29 | { |
29 | { |
| 30 | public: |
30 | public: |
| 31 | Thread() |
31 | Thread() |
| 32 | { |
32 | { |
| 33 | myIsCreated = false; |
33 | myIsCreated = false; |
| 34 | myError = 0; |
34 | myError = 0; |
| 35 | }; |
35 | }; |
| 36 | ~Thread() |
36 | ~Thread() |
| 37 | { |
37 | { |
| 38 | stop(); |
38 | stop(); |
| 39 | }; |
39 | }; |
| 40 | 40 | ||
| 41 | bool stop() |
41 | bool stop() |
| 42 | { |
42 | { |
| 43 | if (!myIsCreated) |
43 | if (!myIsCreated) |
| 44 | return true; |
44 | return true; |
| 45 | 45 | ||
| 46 | myError = pthread_cancel(myHandle); |
46 | myError = pthread_cancel(myHandle); |
| - | 47 | join(); |
|
| 47 | myIsCreated = false; |
48 | myIsCreated = false; |
| 48 | 49 | ||
| 49 | return myError == 0; |
50 | return myError == 0; |
| 50 | }; |
51 | }; |
| 51 | bool start() |
52 | bool start() |
| 52 | { |
53 | { |
| 53 | myError = pthread_create(&myHandle, NULL, T::doThread, (void*)this); |
54 | myError = pthread_create(&myHandle, NULL, T::doThread, (void*)this); |
| 54 | 55 | ||
| 55 | if (myError != 0) |
56 | if (myError != 0) |
| 56 | { |
57 | { |
| 57 | myIsCreated = false; |
58 | myIsCreated = false; |
| 58 | return false; |
59 | return false; |
| 59 | } |
60 | } |
| 60 | 61 | ||
| 61 | myIsCreated = true; |
62 | myIsCreated = true; |
| 62 | return true; |
63 | return true; |
| 63 | }; |
64 | }; |
| 64 | bool join() |
65 | bool join() |
| 65 | { |
66 | { |
| 66 | if (!myIsCreated) |
67 | if (!myIsCreated) |
| 67 | return false; |
68 | return false; |
| 68 | 69 | ||
| 69 | myError = pthread_join(myHandle, NULL); |
70 | myError = pthread_join(myHandle, NULL); |
| 70 | 71 | ||
| 71 | if (myError != 0) |
72 | if (myError != 0) |
| 72 | { |
73 | { |
| 73 | myIsCreated = false; |
74 | myIsCreated = false; |
| 74 | return false; |
75 | return false; |
| 75 | } |
76 | } |
| 76 | 77 | ||
| 77 | myIsCreated = true; |
78 | myIsCreated = true; |
| 78 | return true; |
79 | return true; |
| 79 | }; |
80 | }; |
| 80 | int getError() |
81 | int getError() |
| 81 | { |
82 | { |
| 82 | return myError; |
83 | return myError; |
| 83 | }; |
84 | }; |
| 84 | 85 | ||
| 85 | static void *doThread(void* ptr) |
86 | static void *doThread(void* ptr) |
| 86 | { |
87 | { |
| 87 | ((T*)ptr)->run(); |
88 | ((T*)ptr)->run(); |
| 88 | }; |
89 | }; |
| 89 | void run() { }; |
90 | void run() { }; |
| 90 | 91 | ||
| 91 | private: |
92 | private: |
| 92 | bool myIsCreated; |
93 | bool myIsCreated; |
| 93 | pthread_t myHandle; |
94 | pthread_t myHandle; |
| 94 | int myError; |
95 | int myError; |
| 95 | }; |
96 | }; |
| 96 | 97 | ||
| 97 | #endif /* _THREAD_H */ |
98 | #endif /* _THREAD_H */ |
| 98 | 99 | ||
| 99 | 100 | ||