@@ -47,7 +47,7 @@ class HttpServer {
47
47
48
48
~HttpServer () {
49
49
// Terminate all currently running threads
50
- rtos::ScopedMutexLock lock (threadPoolMutex );
50
+ rtos::ScopedMutexLock lock (thread_pool_mutex );
51
51
52
52
main_thread.terminate ();
53
53
@@ -90,7 +90,7 @@ class HttpServer {
90
90
TCPSocket * socket = nullptr ;
91
91
92
92
{
93
- rtos::ScopedMutexLock lock (threadPoolMutex );
93
+ rtos::ScopedMutexLock lock (thread_pool_mutex );
94
94
auto mapIter = sockets_and_threads.find (rtos::ThisThread::get_id ());
95
95
if (mapIter != sockets_and_threads.end ()) {
96
96
socket = mapIter->second .second .get ();
@@ -157,29 +157,35 @@ class HttpServer {
157
157
nsapi_error_t accept_result;
158
158
TCPSocket* clt_sock = server.accept (&accept_result);
159
159
160
- rtos::ScopedMutexLock lock (threadPoolMutex );
160
+ rtos::ScopedMutexLock lock (thread_pool_mutex );
161
161
162
+ // Create new thread
163
+ Thread* t = nullptr ;
162
164
if (clt_sock != nullptr ) {
163
- // and start listening for events there
164
- Thread* t = new Thread (osPriorityNormal, 2048 );
165
-
166
- // Store thread and socket pointer in map
167
- sockets_and_threads.emplace (t->get_id (), std::make_pair (t, clt_sock));
168
-
169
- t->start (callback (this , &HttpServer::receive_data));
170
- }
171
- else {
172
- printf (" Failed to accept connection: %d\n " , accept_result);
165
+ t = new Thread (osPriorityNormal, 2048 );
173
166
}
174
167
175
- // Find and delete any terminated threads
168
+ // Next, find and delete any terminated threads.
169
+ // This needs to be done first just in case the new thread got the same ID as a previously
170
+ // terminated thread.
176
171
for (auto socketThreadIter = sockets_and_threads.begin (); socketThreadIter != sockets_and_threads.end (); socketThreadIter++) {
177
172
if (socketThreadIter->second .first ->get_state () == Thread::Deleted) {
178
173
CriticalSectionLock criticalSection;
179
174
socketThreadIter = sockets_and_threads.erase (socketThreadIter);
180
175
}
181
176
}
182
177
178
+ // Now, start the thread running. However, note that it won't be able to actually begin executing
179
+ // until we release the thread_pool_mutex lock.
180
+ if (clt_sock != nullptr ) {
181
+ t->start (callback (this , &HttpServer::receive_data));
182
+
183
+ // Once the thread has been started, store it and the socket in the map by its ID.
184
+ sockets_and_threads.emplace (t->get_id (), std::make_pair (t, clt_sock));
185
+ }
186
+ else {
187
+ printf (" Failed to accept connection: %d\n " , accept_result);
188
+ }
183
189
}
184
190
}
185
191
@@ -188,7 +194,7 @@ class HttpServer {
188
194
Thread main_thread;
189
195
190
196
// Mutex which must be locked to create, delete, or iterate threads
191
- rtos::Mutex threadPoolMutex ;
197
+ rtos::Mutex thread_pool_mutex ;
192
198
193
199
// Maps server thread IDs to their thread objects and sockets.
194
200
// unique_ptrs are used so that the objects will get deleted when the map entry is removed.
0 commit comments