File tree 3 files changed +22
-65
lines changed
3 files changed +22
-65
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,28 @@ Thread::Thread(const std::string &name) :
73
73
74
74
Thread::~Thread ()
75
75
{
76
- kill ();
76
+ // kill the thread if running
77
+ if (!m_running) {
78
+ wait ();
79
+ } else {
80
+
81
+ m_running = false ;
82
+
83
+ #if defined(_WIN32)
84
+ // See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method
85
+ TerminateThread ((HANDLE) m_thread_obj->native_handle (), 0 );
86
+ CloseHandle ((HANDLE) m_thread_obj->native_handle ());
87
+ #else
88
+ // We need to pthread_kill instead on Android since NDKv5's pthread
89
+ // implementation is incomplete.
90
+ # ifdef __ANDROID__
91
+ pthread_kill (getThreadHandle (), SIGKILL);
92
+ # else
93
+ pthread_cancel (getThreadHandle ());
94
+ # endif
95
+ wait ();
96
+ #endif
97
+ }
77
98
78
99
// Make sure start finished mutex is unlocked before it's destroyed
79
100
if (m_start_finished_mutex.try_lock ())
@@ -138,37 +159,6 @@ bool Thread::wait()
138
159
}
139
160
140
161
141
- bool Thread::kill ()
142
- {
143
- if (!m_running) {
144
- wait ();
145
- return false ;
146
- }
147
-
148
- m_running = false ;
149
-
150
- #if defined(_WIN32)
151
- // See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method
152
- TerminateThread ((HANDLE) m_thread_obj->native_handle (), 0 );
153
- CloseHandle ((HANDLE) m_thread_obj->native_handle ());
154
- #else
155
- // We need to pthread_kill instead on Android since NDKv5's pthread
156
- // implementation is incomplete.
157
- # ifdef __ANDROID__
158
- pthread_kill (getThreadHandle (), SIGKILL);
159
- # else
160
- pthread_cancel (getThreadHandle ());
161
- # endif
162
- wait ();
163
- #endif
164
-
165
- m_retval = nullptr ;
166
- m_joinable = false ;
167
- m_request_stop = false ;
168
-
169
- return true ;
170
- }
171
-
172
162
173
163
bool Thread::getReturnValue (void **ret)
174
164
{
Original file line number Diff line number Diff line change @@ -74,14 +74,6 @@ class Thread {
74
74
*/
75
75
bool stop ();
76
76
77
- /*
78
- * Immediately terminates the thread.
79
- * This should be used with extreme caution, as the thread will not have
80
- * any opportunity to release resources it may be holding (such as memory
81
- * or locks).
82
- */
83
- bool kill ();
84
-
85
77
/*
86
78
* Waits for thread to finish.
87
79
* Note: This does not stop a thread, you have to do this on your own.
Original file line number Diff line number Diff line change @@ -31,7 +31,6 @@ class TestThreading : public TestBase {
31
31
void runTests (IGameDef *gamedef);
32
32
33
33
void testStartStopWait ();
34
- void testThreadKill ();
35
34
void testAtomicSemaphoreThread ();
36
35
};
37
36
@@ -40,7 +39,6 @@ static TestThreading g_test_instance;
40
39
void TestThreading::runTests (IGameDef *gamedef)
41
40
{
42
41
TEST (testStartStopWait);
43
- TEST (testThreadKill);
44
42
TEST (testAtomicSemaphoreThread);
45
43
}
46
44
@@ -111,29 +109,6 @@ void TestThreading::testStartStopWait()
111
109
}
112
110
113
111
114
- void TestThreading::testThreadKill ()
115
- {
116
- SimpleTestThread *thread = new SimpleTestThread (300 );
117
-
118
- UASSERT (thread->start () == true );
119
-
120
- // kill()ing is quite violent, so let's make sure our victim is sleeping
121
- // before we do this... so we don't corrupt the rest of the program's state
122
- sleep_ms (100 );
123
- UASSERT (thread->kill () == true );
124
-
125
- // The state of the thread object should be reset if all went well
126
- UASSERT (thread->isRunning () == false );
127
- UASSERT (thread->start () == true );
128
- UASSERT (thread->stop () == true );
129
- UASSERT (thread->wait () == true );
130
-
131
- // kill() after already waiting should fail.
132
- UASSERT (thread->kill () == false );
133
-
134
- delete thread;
135
- }
136
-
137
112
138
113
class AtomicTestThread : public Thread {
139
114
public:
You can’t perform that action at this time.
0 commit comments