Skip to content

Commit b4df318

Browse files
Thread ID is 0 before the thread starts
1 parent 4ffca31 commit b4df318

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/http_server.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HttpServer {
4747

4848
~HttpServer() {
4949
// Terminate all currently running threads
50-
rtos::ScopedMutexLock lock(threadPoolMutex);
50+
rtos::ScopedMutexLock lock(thread_pool_mutex);
5151

5252
main_thread.terminate();
5353

@@ -90,7 +90,7 @@ class HttpServer {
9090
TCPSocket * socket = nullptr;
9191

9292
{
93-
rtos::ScopedMutexLock lock(threadPoolMutex);
93+
rtos::ScopedMutexLock lock(thread_pool_mutex);
9494
auto mapIter = sockets_and_threads.find(rtos::ThisThread::get_id());
9595
if(mapIter != sockets_and_threads.end()) {
9696
socket = mapIter->second.second.get();
@@ -157,29 +157,35 @@ class HttpServer {
157157
nsapi_error_t accept_result;
158158
TCPSocket* clt_sock = server.accept(&accept_result);
159159

160-
rtos::ScopedMutexLock lock(threadPoolMutex);
160+
rtos::ScopedMutexLock lock(thread_pool_mutex);
161161

162+
// Create new thread
163+
Thread* t = nullptr;
162164
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);
173166
}
174167

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.
176171
for (auto socketThreadIter = sockets_and_threads.begin(); socketThreadIter != sockets_and_threads.end(); socketThreadIter++) {
177172
if (socketThreadIter->second.first->get_state() == Thread::Deleted) {
178173
CriticalSectionLock criticalSection;
179174
socketThreadIter = sockets_and_threads.erase(socketThreadIter);
180175
}
181176
}
182177

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+
}
183189
}
184190
}
185191

@@ -188,7 +194,7 @@ class HttpServer {
188194
Thread main_thread;
189195

190196
// Mutex which must be locked to create, delete, or iterate threads
191-
rtos::Mutex threadPoolMutex;
197+
rtos::Mutex thread_pool_mutex;
192198

193199
// Maps server thread IDs to their thread objects and sockets.
194200
// unique_ptrs are used so that the objects will get deleted when the map entry is removed.

0 commit comments

Comments
 (0)